Thread: Reading a file (one double number in each line)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Reading a file (one double number in each line)

    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

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    More like %lf

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Oops! You're right of course nonoob.
    Note to the OP: that's a lowercase L in front of the f: %lf

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    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.
    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);
        }
    }

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to use the address operator '&'
    Code:
    fscanf(file1, "%lf", &bb);

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by MK27 View Post
    "f" indicates a double or a float, "lf" is undefined behaviour, "Lf" is a long double).
    Are you sure it's undefined, my man page here (BSD) says that the "l" modifier is ignored for "f", same behavior as without it.

    Quote Originally Posted by printf manpage
    The following length modifier is valid for the a, A, e, E, f, F, g, or G conversion:

    Modifier a, A, e, E, f, F, g, G
    l (ell) double (ignored, same behavior as without it)
    L long double

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Subsonics View Post
    Are you sure it's undefined, my man page here (BSD) says that the "l" modifier is ignored for "f", same behavior as without it.
    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

    Code:
     * 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.
    Someone lambasted me here for this a few months ago
    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

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by nonoob View Post
    You need to use the address operator '&'
    Code:
    fscanf(file1, "%lf", &bb);
    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);
        }
    }

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by zmarcoz View Post
    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?
    You solve it by more carefully reading the previous responses people gave you. In particular, oogabooga's response in post #9 about checking the return value of fscanf.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    You solve it by more carefully reading the previous responses people gave you. In particular, oogabooga's response in post #9 about checking the return value of fscanf.
    Good blame. Solved .............

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Reading Line With Unknown Number of Integers
    By envec83 in forum C Programming
    Replies: 3
    Last Post: 09-27-2011, 11:20 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  4. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM

Tags for this Thread