Thread: Reading and using numbers from a file

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Question Reading and using numbers from a file

    Hi all,

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Sorry for premature posting

    I need to process data from a file with the following format:

    Code:
    41.877430 0.200000 0.200000 1.000000 1 
    42.339146 0.200000 0.200000 0.400000 1 
    42.507645 1.000000 1.000000 0.400000 1 
    43.607258 0.500000 0.500000 0.200000 1
    All I really need is the three last numbers. I need to read them, and process them until the end of the file.

    I tried the following program:

    Code:
    FILE *infile;
    
    int main(void)
    {float sim_time, p, q, s,sum;
     int accepted;
       
         infile  = fopen("temp_log", "w");
    
    
        while(!feof(infile)) { 
          /* loop through and store the numbers into the array */
          fscanf(infile, "%f %f %f %f %i", &sim_time, &p, &q, &s, &accepted);
            
          sum += complex_function(q,s,accepted);
    
          }
    
        printf("The sum is %f \n" ,sum); 
    
     fclose(infile);
    }
    But it does not work. In particular, I am not sure how to read the numbers, how to start a new line, and how to know when I reach the end of the file.

    Thanks for any ideas.

    Tor

Popular pages Recent additions subscribe to a feed