Thread: Reading values from external text file

  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    3

    Reading values from external text file

    I have an assignment where I need to read values from an external text file and do some computations with them. The file is a control number file, with 22 lines and three numbers on each line. I have no problem setting up the file pointers, reading the file and storing numbers from the file in variables. My issue is that I need to take the sum of all the third numbers from each line. Searching the internet gave me lots of examples using fgets() and sscanf() etc. We haven't learned those in class yet, so I'm not using those. We've discussed loops (for, while etc.) and if statements. I'm just stumped as to how I might get those numbers and add them up to be used in a calculation. The formula is something like X(Y1+Y2+Y3.....) with the Y values being that third number from each line. It seems that whenever I uses the fscanf function, which is the only file scanning function I know, it reads the first line only.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well post the code you have that uses fscanf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2023
    Posts
    3
    Quote Originally Posted by Salem View Post
    Well post the code you have that uses fscanf.
    Nevermind, I figured that out. My problem with the code now is the calculations are off. I initially had the calculations within the main function and they worked fine, but moved them to user defined functions below the main. Initially when I did this I got a ton of errors so changed some variable names in the user defined functions. Now the numbers are off, and I can't figure out why. They should read:

    ROCKET MOTOR TEST RESULTS


    Total Impulse: 217.1 lb-sec
    Specific Impulse: 141.0 sec
    Average Thrust: 103.4 lb

    But are now reading:

    ROCKET MOTOR TEST RESULTS


    Total Impulse: 217.0 lb-sec
    Specific Impulse: 140.0 sec
    Average Thrust: 206.0 lb

    I didn't change the calculations, just moved them. This is my first time attempting the user defined functions and call backs, so I'm not sure what in the mix is throwing this off.


    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <math.h>
    #define prop_mass 1.54
    #define infile "C:\\Users\\tommc\\OneDrive\\Documents\\ENGR 200\\\
    time-thrust.txt"
    #define outfile "C:\\Users\\tommc\\OneDrive\\Documents\\ENGR 200\\\
    impulse-thrust.txt"
    
    /* function prototypes */
    int get_i_t(double);
    int get_i_sp(double);
    int get_f_avg(double);
    
    /* Main function */
    int main(void)
    {
       /* Declare variables */
       double i_t, calc_it, calc_sp, calc_f_avg, i_sp, f_avg, delta_t, t, F, f_tot,
       t_tot;
       int i, ndata;
       FILE *time=NULL, *impulse=NULL;
       
       /* open files */
       
       time=fopen(infile, "r");
       impulse=fopen(outfile, "w");
    
       
       /* Print headings */
       printf("********************************************");
       printf("\n         ROCKET MOTOR TEST RESULTS");
       fprintf(impulse,"********************************************");
       fprintf(impulse,"\n         ROCKET MOTOR TEST RESULTS");
       
       /* Verify input file and terminate program if file not found */
       
       if(time==NULL)
       {
           printf("\n\n\n\n ERROR OPENING INPUT FILE.");
           printf("\n\n PROGRAM TERMINATED.\n\n\n");
           return 1;
       }
       
       /* Read control number */
       
       fscanf(time, "%i", &ndata);
             
           /* Calculate specific impulse and average thrust */
           
           i_t = get_i_t(calc_it);
           i_sp = get_i_sp(calc_sp);
           f_avg = get_f_avg(calc_f_avg);
           
       /* Print output values */
       
       printf("\n\nTotal Impulse:      %5.1lf", i_t);
       printf(" lb-sec");
       printf("\nSpecific Impulse:   %5.1lf", i_sp);
       printf(" sec");
       printf("\nAverage Thrust:     %5.1lf", f_avg);
       printf(" lb");
       printf("\n********************************************\n\n\n");
       fprintf(impulse, "\n\nTotal Impulse:      %5.1lf", i_t);
       fprintf(impulse, " lb-sec");
       fprintf(impulse, "\nSpecific Impulse:   %5.1lf", i_sp);
       fprintf(impulse, " sec");
       fprintf(impulse, "\nAverage Thrust:     %5.1lf", f_avg);
       fprintf(impulse, " lb");
       fprintf(impulse, "\n********************************************\n\n\n");
       
       /* Close the files */
       fclose(time);
       fclose(impulse);
    
       /* Exit the program */
       return 0;
    }
    
    get_i_t(double calc_it)
    {
    double calc_i_t, delta_t, F, t;
    int i, ndata;
    FILE *time=NULL;
    time=fopen(infile, "r");
    
    fscanf(time, "%i", &ndata);
    
    for (i = 0; i <= ndata; i++)
        {
        fscanf(time, "%lf,%lf,%lf", &t, &delta_t, &F);
        calc_it += delta_t * F;
        }
        
    fclose(time);
    
    return calc_it;    
    }
    
    get_i_sp(double calc_sp)
    {
     /* Declare variables */
       double i_t, i_sp, delta_t, F, t;
       int i, ndata;
       FILE *time=NULL;
       
       time=fopen(infile, "r");
       
       fscanf(time, "%i", &ndata);
       
    for (i = 0; i <= ndata; i++)
       {
       fscanf(time, "%lf,%lf,%lf", &t, &delta_t, &F);
       i_t += delta_t * F;
       }
    calc_sp = i_t / prop_mass;
    fclose(time);
    return calc_sp;
    }
    
    get_f_avg(double f_avg)
    {
    /* Declare variables */
    
       double i_t, delta_t, t, F, t_tot, calc_f_avg;
       int i, ndata;
       FILE *time=NULL;
       time=fopen(infile, "r");
       fscanf(time, "%i", &ndata);
       
    for (i = 0; i <= ndata; i++)
        {
        fscanf(time, "%lf,%lf,%lf", &t, &delta_t, &F);
        i_t += delta_t * F;
        t_tot = t;
        }
    calc_f_avg = i_t / t_tot;
    fclose(time);
    return calc_f_avg;
    }
    Also, this is the contents of the external file

    22
    0.0,0.0,0.0
    0.1,0.1,50.0
    0.2,0.1,100.0
    0.3,0.1,140.0
    0.4,0.1,150.0
    0.5,0.1,155.0
    0.6,0.1,155.0
    0.7,0.1,154.0
    0.8,0.1,150.0
    0.9,0.1,149.0
    1.0,0.1,148.0
    1.1,0.1,140.0
    1.2,0.1,138.0
    1.3,0.1,132.0
    1.4,0.1,120.0
    1.5,0.1,100.0
    1.6,0.1,80.0
    1.7,0.1,50.0
    1.8,0.1,30.0
    1.9,0.1,20.0
    2.0,0.1,10.0
    2.1,0.1,0.0
    Last edited by tommcgtx; 03-10-2023 at 07:01 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (i = 0; i <= ndata; i++)
    Every one of your for loops should use <, not <=

    You're going round the loops 23 times instead of 22, resulting in an extra calculation with potentially garbage values from the fscanf.

    There are multiple warnings that need to be fixed, like using uninitialised variables and mis-matched return types.
    In fact, it looks like all your functions should return doubles, not ints.

    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:21:8: warning: unused variable ‘i’ [-Wunused-variable]
       21 |    int i, ndata;
          |        ^
    foo.c:20:4: warning: unused variable ‘t_tot’ [-Wunused-variable]
       20 |    t_tot;
          |    ^~~~~
    foo.c:19:74: warning: unused variable ‘f_tot’ [-Wunused-variable]
       19 |    double i_t, calc_it, calc_sp, calc_f_avg, i_sp, f_avg, delta_t, t, F, f_tot,
          |                                                                          ^~~~~
    foo.c:19:71: warning: unused variable ‘F’ [-Wunused-variable]
       19 |    double i_t, calc_it, calc_sp, calc_f_avg, i_sp, f_avg, delta_t, t, F, f_tot,
          |                                                                       ^
    foo.c:19:68: warning: unused variable ‘t’ [-Wunused-variable]
       19 |    double i_t, calc_it, calc_sp, calc_f_avg, i_sp, f_avg, delta_t, t, F, f_tot,
          |                                                                    ^
    foo.c:19:59: warning: unused variable ‘delta_t’ [-Wunused-variable]
       19 |    double i_t, calc_it, calc_sp, calc_f_avg, i_sp, f_avg, delta_t, t, F, f_tot,
          |                                                           ^~~~~~~
    foo.c: At top level:
    foo.c:80:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
       80 | get_i_t(double calc_it)
          | ^~~~~~~
    foo.c: In function ‘get_i_t’:
    foo.c:82:8: warning: unused variable ‘calc_i_t’ [-Wunused-variable]
       82 | double calc_i_t, delta_t, F, t;
          |        ^~~~~~~~
    foo.c: At top level:
    foo.c:100:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
      100 | get_i_sp(double calc_sp)
          | ^~~~~~~~
    foo.c: In function ‘get_i_sp’:
    foo.c:103:16: warning: unused variable ‘i_sp’ [-Wunused-variable]
      103 |    double i_t, i_sp, delta_t, F, t;
          |                ^~~~
    foo.c: At top level:
    foo.c:121:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
      121 | get_f_avg(double f_avg)
          | ^~~~~~~~~
    foo.c: In function ‘main’:
    foo.c:51:14: warning: ‘calc_it’ may be used uninitialized [-Wmaybe-uninitialized]
       51 |        i_t = get_i_t(calc_it);
          |              ^~~~~~~~~~~~~~~~
    foo.c:52:15: warning: ‘calc_sp’ may be used uninitialized [-Wmaybe-uninitialized]
       52 |        i_sp = get_i_sp(calc_sp);
          |               ^~~~~~~~~~~~~~~~~
    foo.c:53:16: warning: ‘calc_f_avg’ may be used uninitialized [-Wmaybe-uninitialized]
       53 |        f_avg = get_f_avg(calc_f_avg);
          |                ^~~~~~~~~~~~~~~~~~~~~
    > t_tot = t;
    I think you meant += here, if you want a total.

    Also, there is no longer any point to opening the input file in main.
    Each function now handles the input file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2023
    Posts
    3
    Thanks Salem. I edited it further and turned it in. I will look at your post in detail tomorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem writing to and reading from external text file
    By stuarte83 in forum C Programming
    Replies: 10
    Last Post: 11-14-2022, 08:42 PM
  2. Reading specific values in text file - C
    By kdan in forum C Programming
    Replies: 4
    Last Post: 09-03-2016, 05:38 PM
  3. Reading values from text file
    By a.mlw.walker in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 06:02 PM
  4. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  5. Replies: 6
    Last Post: 11-11-2006, 02:10 PM

Tags for this Thread