Dear all,
I need to read a file which has unknown number of lines, but each line is just 1 double number. How I can do that?
Please help
Thanks
Dear all,
I need to read a file which has unknown number of lines, but each line is just 1 double number. How I can do that?
Please help
Thanks
Welllll... you could put on your glasses, make a nice cup of coffee and sit to do a little reading.
Or... you could get started on it, get the project as far as you can... then post your code and ask specific questions.
As tater said, you have to post an attempt. Look into fscanf. It's a little complicated, but the format specifier you want is f.
More like %lf
Oops! You're right of course nonoob.
Note to the OP: that's a lowercase L in front of the f: %lf
You are right! I should post my code first. Here is my unworking code. In the code, I assume there is less than 100 double values.
Code:#include <stdio.h> #include <math.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, char *argv[]) { FILE *file1 = fopen( argv[1], "r" ); if ( argc !=2 ) /* argc should be 5 for correct execution */ { printf( "Error of input argument number" ); } int i; double bb; for (i=1;i<100;i++){ fscanf(file1, "%f",bb); printf("%f",bb); } }
You should use %lf in the scan, but %f in the print, they have slightly different rules (for scan, "f" indicates a float, "lf" a double, "Lf" a long double; for print, "f" indicates a double or a float, "lf" is undefined behaviour, "Lf" is a long double).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You need to use the address operator '&'
Code:fscanf(file1, "%lf", &bb);
You need to ensure that the file has been opened after the fopen.
If "argc should be 5 for correct execution" then wy are you testing it against 2?
Your loop control assumes that there are going to be exactly 99 doubles in the file. What if there are less? You need to test fscanf for a return value of 1. If it returns something else, exit the loop.
These two have already been mentioned:
You need to use "%lf" as the format specifier for a double.
You need to pass the address of bb, not its value. So use &bb.
Hmm, well, man pages aren't exactly implementation specific, but I'd guess most implementations do behave that way, and it was incorporated into C99 ("an optional l which has no effect"). The POSIX specification is the same, and perhaps the BSD manual assumes POSIX.
But for ANSI C / ISO C89, it is undefined.
http://noserose.net/e/C89/ansi.c.txt
4.9.6.1 The fprintf function
Someone lambasted me here for this a few months agoCode:* An optional h specifying that a following d , i , o , u , x , or X conversion specifier applies to a short int or unsigned short int argument (the argument will have been promoted according to the integral promotions, and its value shall be converted to short int or unsigned short int before printing); an optional h specifying that a following n conversion specifier applies to a pointer to a short int argument; an optional l (ell) specifying that a following d , i , o , u , x , or X conversion specifier applies to a long int or unsigned long int argument; an optional l specifying that a following n conversion specifier applies to a pointer to a long int argument; or an optional L specifying that a following e , E , f , g , or G conversion specifier applies to a long double argument. If an h , l , or L appears with any other conversion specifier, the behavior is undefined.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Thanks, I updated my code, but there is another problem. Since I assume that the length is 100 in the file. I created a text file which only have 3 double values, but my program repeat the last value for many times (I guest 98 times). How I can solve it? The following is the new code.
Code:#include <stdio.h> #include <math.h> #include <sys/types.h> #include <sys/stat.h> int main(int argc, char *argv[]) { int i; double bb; FILE *file1 = fopen( argv[1], "r" ); if ( argc !=2 ) /* argc should be 2 for correct execution */ { printf( "Error of input argument number" ); } for (i=1;i<100;i++){ fscanf(file1, "%lf",&bb); printf("%f",bb); } }