C - read and store data file for further calculations

后端 未结 3 1574
谎友^
谎友^ 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!
    0 讨论(0)
  • 2021-01-28 10:10

    An example for writing:

    FILE *fp;
    fp=fopen("c:\\test.bin", "wb");
    char x[10]="ABCDEFGHIJ";
    fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
    

    (write declaration)

    size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
    

    If you were to read,

    size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
    
    0 讨论(0)
  • 2021-01-28 10:24

    You'll need to create a data structure to hold them and then a vector of this struct which will hold each currency read. You should make use of the fscanf function that will not only save you from splitting values by hand but will also convert them for you. Here is something I came up with:

    /* to store each currency you read */
    struct currency {
      char name[256];
      double value;
    };
    
    int main(int argc, char ** argv) {
      FILE * fp = fopen("x.dat", "r");
      int count = 0, i;
      struct currency currencies[30];
    
      /* this will read at most 30 currencies, and stop in case a
       * end of file is reached */
      while (count < 30 && !feof(fp)) {
        /* fscanf reads from fp and returns the amount of conversions it made */
        i = fscanf(fp, "%s %lf\n", currencies[count].name, &currencies[count].value);
    
        /* we expect 2 conversions to happen, if anything differs
         * this possibly means end of file. */
        if (i == 2) {
          /* for the fun, print the values */
          printf("got %s %lf\n", currencies[count].name, currencies[count].value);
          count++;
        }
      }
      return 0;
    }
    

    To read them again, you'll need to iterate on the currencies array until you reach count iterations.

    Since you already wants to match those values with the strcmp function, read the currency name, iterate on the array until you find a match and then perform calculations on those.

    This is basic C knowledge and as much as I understand you're not used to using it, I strongly suggest you read a book to find these answers in them.

    0 讨论(0)
提交回复
热议问题