C - read and store data file for further calculations

后端 未结 3 1580
谎友^
谎友^ 2021-01-28 09:33

I normally use R, and have a lot of trouble understanding C. I need to read and store a data file, shown below, so that I can perform calculations on the data.

3条回答
  •  故里飘歌
    2021-01-28 10:08

    I suggest you

    • define a struct like :

    typedef struct{ char currency[10]; double rate; }rate_currency;

    • getline:

    in your main function you use getline to read the file line by line :

    while ((read = getline(&line, &len, fpt)) != -1) ...
    
    • Separate:

      use strchr to search for the space character to separate currency name from currency rate

    • Insert :

    declare an array of your previous struct :

    rate_currency arrOfStruct[10]; then, insert one by one for example :

    arrOfStruct[0].currency = "dollar"; //after you read it and separated it... arrOfStruct[0].rate = 1.00;

    • You're Done!

提交回复
热议问题