0

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. These calculations depend on user-entered infomation. Here's the data that I'm trying to read in (called "Downloads/exchange.dat" in my code),

dollar   1.00
yen      0.0078
franc    0.20
mark     0.68
pound    1.96

Here's where I'm at so far. This reads the first line of data only and also returns it, which is not what I want. I need to read this entire file, store the exchange rates with their respective currencies, and be able to perform calculations on them later.

I think I need a typedef, perhaps? Later in the program, I ask the user for information like "Convert from?" and "convert to?". In which case, they'll enter "mark", "yen", "dollar", etc. and I hope to match their response to the respective exchange rate (using the string.h library).

My code so far, to read in the data:

#include <stdio.h>

main(int argc, char *argv[])
{
    FILE *fpt;  // define a pointer to pre-defined structure type FILE
    char c;

    // open the data file for reading only
    if ((fpt = fopen("Downloads/exchange.dat", "r")) == NULL)
        printf("\nERROR - Cannot open the designated file\n");

    else   // read and display each character from the data file
        do
            putchar(c = getc(fpt));
        while (c != '\n');

    // close the data file
    fclose(fpt);
}

I feel like I need something similar, but completely different to read and store entire data file. Your help is appreciated.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245

3 Answers3

2

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.

Community
  • 1
  • 1
Leonardo
  • 1,834
  • 1
  • 17
  • 23
  • Excellent. Thanks a lot. Thanks for the link too. I have a couple of books but they're crap and hence not on those lists. – Rich Scriven Jun 05 '14 at 01:18
0

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);
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
0

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!
chouaib
  • 2,763
  • 5
  • 20
  • 35
  • I advise against getline because it is a POSIX function and might not be available on other non-POSIX platforms. – Leonardo Jun 05 '14 at 01:09
  • +1 to your answer though I don't recommend complete answers, the OE must be left with a hint and some work to do, not only ctrl+c ctrl+v ;) – chouaib Jun 05 '14 at 01:27