parsing - c language : read file content as numbers and add them together -
i have following in text file called: values.txt
1 4 2.5 3.76 122 10 277.543 165.4432
i trying read content of text file, , add each 2 pairs , output result ... output :
1 pair:(1, 4) = 5 2 pair:(2.5, 3.76)= 6.26
and on ..
i opening file
int c; file myfile; myfile= fopen("values.txt", "r"); if ( myfile == null ) { printf("cannot open text file\n"); return 1; } double aa,bb; while ( (c = getc(myfile) ) != eof ) { // here should output how? }
any appreciated ..
language = c
the following code expect. myfile should declared file*. fopen returns pointer file structure. if file large, recommend reading in buffers of big size (eg: 65535 etc) , parse char char , convert float values. reduces system call overhead takes more time processing text float values.
#include <stdio.h> #include <string.h> main(int argc, char *argv[]) { file* myfile; myfile = fopen("values.txt", "r"); if ( myfile == null ) { printf("cannot open text file\n"); return 1; } double aa,bb; while (2 == fscanf(myfile, "%lf %lf", &aa, &bb)) { printf("%lf\n", aa+bb); } return 0; }
Comments
Post a Comment