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.