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!