Hello Dani Web Forum,
I'm really new to C. I have an assignment, which I think it's very hard to solve. I have to create a Currency Converter Program that reads data from txt files. The program should also be able to read arguments (argv[]). I'm really confused since it's just a plain text. How can I read it as a database in C?

The data, which on the "currency.txt" saved with:

typedef struct{
char date[11];
char curr[10];
double buying;
double selling;
}CURRENCY;

contains:
--------------------------------------------------
DATE | currency | buying rate | selling rate
*not stated in "currency.txt"
--------------------------------------------------
*actual data:

18.10.2011	USD/TRY		1.8578		1.8668
18.10.2011	AUD/TRY		1.8832		1.8955
18.10.2011	DKK/TRY		0.34176		0.34344
18.10.2011	EUR/TRY		2.5448		2.5571
18.10.2011	CHF/TRY		2.0582		2.0715
18.10.2011	SEK/TRY		0.27622		0.27909
18.10.2011	CAD/TRY		1.8152		1.8234
18.10.2011	KWD/TRY		6.6759		6.7638
18.10.2011	NOK/TRY		0.32820		0.33041
18.10.2011	SAR/TRY		0.49687		0.49777
18.10.2011	JPY/TRY		2.4164		2.4325
18.10.2011	BGN/TRY		1.2904		1.3074
18.10.2011	RON/TRY		0.57992		0.58756
18.10.2011	RUB/TRY		0.05904		0.05982
18.10.2011	IRR/TRY		0.01728		0.01751
19.10.2011	USD/TRY		1.8208		1.8296
19.10.2011	AUD/TRY		1.8335		1.8455
19.10.2011	DKK/TRY		0.33633		0.33799
19.10.2011	EUR/TRY		2.5043		2.5164
19.10.2011	GBP/TRY		2.8589		2.8738
19.10.2011	CHF/TRY		2.0192		2.0322
19.10.2011	SEK/TRY		0.27314		0.27598
19.10.2011	CAD/TRY		1.7860		1.7941
19.10.2011	KWD/TRY		6.5428		6.6290
19.10.2011	NOK/TRY		0.32138		0.32355
19.10.2011	SAR/TRY		0.48695		0.48783
19.10.2011	JPY/TRY		2.3702		2.3859
19.10.2011	BGN/TRY		1.2698		1.2865
19.10.2011	RON/TRY		0.57466		0.58223
19.10.2011	RUB/TRY		0.05789		0.05865
19.10.2011	IRR/TRY		0.01707		0.01729
----------------------------------------------------------

Until now, I just be able to convert whether it is buying rate or selling rate.

#include <stdio.h>
#include <stdlib.h>

typedef int ExchangeRate;
enum ExchangeRate {Buying = 1, Selling};
int main (int argc, char *argv[])
{
int amount = atoi(argv[3]);/* 3rd argument */
FILE *prabuinputfile, *prabuoutputfile; /* pointer for a file */
prabuinputfile = fopen("currency.txt" , "r"); /* Opening a source data file */
prabuoutputfile = fopen("temporaryresult.txt", "w"); /* Creating temporary files for storing data */
 typedef struct{
   char date[11];
   char curr[10];
   double buying;
   double selling;
   
} CURRENCY;
    char *date=argv[1];/* 1st argument */
    char *curr=argv[2];/* 2nd argument */
    ExchangeRate i = atoi(argv[4]);/* 4th argument */

CURRENCY Try;
/*double rb=(Try.buying*amount);
double rs=(Try.selling*amount);*/
          if ((prabuinputfile = fopen("currency.txt", "r")) == NULL) /* if no file */
              printf("File could not be opened.\n");
          else
              while (fscanf(prabuinputfile, "%s %s %lf %lf \n", &Try.date, &Try.curr, &Try.buying, &Try.selling) !=EOF) /* reads until end of file */
                 if ((i == 1) && (amount>=0)) fprintf(prabuoutputfile, "%.3lf\n", Try.buying*amount);
                 else if ((i == 2) && (amount >= 0)) fprintf(prabuoutputfile, "%.3lf\n", Try.selling*amount);
                 else printf("choice invalid");
                  fclose(prabuinputfile);
                  fclose(prabuoutputfile);
}

The code above will results all data either buying rates * amount or selling rate *amount, like this:
It is only can be opened using arguments.

$user\myprogram 18.10.2011 USD 100 2
18.10.2011 = argument 1 = date
USD = argument 2 = currency
100 = argument 3 = amount of money
1 = argument 4 = (1 = buying, 2 = selling)
the result:
-------------------
186.680
189.550
34.344
255.710
207.150
27.909
182.340
676.380
33.041
49.777
243.250
130.740
58.756
5.982
1.751
182.960
184.550
33.799
251.640
287.380
203.220
27.598
179.410
662.900
32.355
48.783
238.590
128.650
58.223
5.865
1.729
----------------------

Actually, it will be described as correct program if I input:
Sample run 1 :
$user\myprogram 18.10.2011 EUR 100 1

Real output should be like this:

Output 1:
254.48

Only one output, which not read all the data like mine did.

I wonder how can I read only 1 line of them?

Please Help me.. Thank you very much!

Edited 5 Years Ago by mprabuw: n/a

Welcome to Daniweb :)

Well, you can use a string comparison function. Use strcmp() if allowed or write your own version of it.

In this:

while (fscanf(prabuinputfile, 
       "%s %s %lf %lf \n", 
       &Try.date, &Try.curr, &Try.buying, &Try.selling) !=EOF
       )
{
  // Add if condition here.
}

So, do a string comparison this way:

if (!strcmp(argv[1], Try.curr)) {
   // Match. Terminate loop.
}

strcmp


Hope this helps.

**PS: There seems to be a "/TRY" part in each second field. So, you could perhaps concatenate that with Try.curr and then do a string compare. Kindly let me know if i have understood the problem correctly.

Edited 5 Years Ago by myk45: n/a

Thank you for the help. :). Actually I haven't been taught about that strcmp yet. But I'll read more about it then.

Hmm, right now I'm still working about the argument. It's because I think that my argument for the first and the second don't work.

Next step, when I'm successfully define the first and the second argument, I'll use 'if else if' function to check the date and currency. The problem is, there will be so many 'if else if' statement there, since there are 2 dates and 15 currency. Probably, at least, there will be 17 'if else if' function there.

Please correct me if I'm wrong with the logic.

However, can I use fseek or fread to this problem? For example:

while (fscanf(ftemp, "%s %s %lf %lf \n", &Try.date, &Try.curr, &Try.buying, &Try.selling) !=EOF)
{
   if ((argv[1] == "18.10.2011") && (argv[2] == "USD")){
       fseek(prabuoutputfile, 0, SEEK_SET); 
       fread(&result, 0, 0, ftemp);
       printf("%s\n", result);
}

and this one?

else if ((Try.date == "18.10.2011") && (Try.curr == "AUD")){
         fseek(ftemp, 0, SEEK_SET); 
         fread(&result, 35, 1, ftemp);
         printf("%s\n", result);
}
/* next else if will check the date and curr. There are so many else if btw */

You do NOT need fseek() . At the beginning of the program, read the data into an array of your CURRENCY structure and you are done with the file. Then you can process all the data you want.

Just seconds ago, I did it! However, I'm using fseek and fgets and using 72 (36 x 2) 'if else if' functions! Wow! So many.. I know it's not efficient.. It's because when the currency.txt structure is changed, it won't be able to read the data correctly..
This is one of many 'if else if' code:

else if ((!strcmp(argv[1], date1)) && (!strcmp(argv[2], curr9))) {
fseek(fsource, 324, SEEK_SET);
fread(&Record, 20, 1, fsource);
if ((i == 1) && (amount>=0)) printf("%.3lf\n", Record.buying*amount);
    else if ((i == 2) && (amount >= 0)) printf("%.3lf\n", Record.selling*amount);
    else printf("Choice Invalid!\n");
    return 0;

The most important thing I'm able to finish it.
horraayyy! so happy to learn C! Thank you all who tried to help me.. :). I believe there is code which truly correct. I'm really happy if there's someone would like to share it.

Edited 5 Years Ago by mprabuw: n/a

You do NOT need fseek() . At the beginning of the program, read the data into an array of your CURRENCY structure and you are done with the file. Then you can process all the data you want.

@WaltP: You were right about the fseek. I'm still have a problem! I was wrong. I't only correctly open the first line of the data. But the rest of them are unreadable.. about how to acquire the data to be the arrays then? Please tell me in specific since I'm new to C. Please.. What functions should I use? At least, please tell me how to read each line of the data when there is string and double file type data at the same time?

As you can see, in each line, there is
date | currency | buying rate | selling rate

date for string
currency for string
buying rate for double
selling rate for double

When using fseek, I'm unable to read the data with fread. The data will be somehow unreadable and showing some strange character. Should I use Fgets?

I was using fscanf:

fscanf(fsource, "%s %s %ld %ld", &Record.date, &Record.curr, &Record.buying, &Record.selling);
printf("%s %s %lf %lf\n", Record.date, Record.curr, Record.buying, Record.selling);

But, still, the data printed on the dos still incorrect! What should I do? *stressed*

Edited 5 Years Ago by mprabuw: n/a

solved! :)
just changing %ld in fscanf:

fscanf(fsource, "%s %s %ld %ld", &Record.date, &Record.curr, &Record.buying, &Record.selling);
    printf("%s %s %lf %lf\n", Record.date, Record.curr, Record.buying, Record.selling);

into %lf

fscanf(fsource, "%s %s %lf %lf", &Record.date, &Record.curr, &Record.buying, &Record.selling);
    printf("%s %s %lf %lf\n", Record.date, Record.curr, Record.buying, Record.selling);
This question has already been answered. Start a new discussion instead.