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

Hybrid View

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

  2. #2
    - - - - - - - - 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.

  3. #3
    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);
        }
    }

  4. #4
    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.

  5. #5
    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