Thread: Please Help Reading Data From A File

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Please Help Reading Data From A File

    Hi everybody,

    I have an input file called cond.inp
    The file contains data in the form as follows:

    Number:1 Species: first Concentration: 1.0e14 Density: 4.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
    Number:2 Species: second Concentration: 1.0e14 Density: 2.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
    Number:3 Species: third Concentration: 1.0e14 Density: 1.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
    Number:4 Species: fourth Concentration: 1.0e14 Density: 4.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0


    As you can see there are a a number of lines that contains a series of names with values. Each line has the same format, although the length of the Species value is not set.
    Could some please tell me how I can read the data into a series of arrays.

    I have the following code and it works to some degree:

    Code:
    //part of program that opens the required file
    FILE *fptr1;
      if((fptr1=fopen("cond.inp","r"))==NULL)//Opening the input file that contains property data for the aerosol material.
      {
        printf("Warning: Could not open file");
        return(-1);
      }
      //Scanning the property data from the input file "cond.inp"
    
      fscanf(fptr1,"Number: %d Species: %c Concentration: %e Density: %lf Molecular Weight: %lf Production Rate: %lf Saturation Vapor Pressure: C=%lf D=%lf Surface Tension: A=%lf B=%lf ",&h,&conname[1],&conconc[1],&conrho[1],&conmw[1],&conprod[1],&C[1],&D[1],&A[1],&B[1]);
    
      fclose(fptr1);//Closing the main property data file.
    The code works for reading the first line of data. However I have tried to read the subsequent data in by repeating the Fscan statement seven times but the subsequent data values were not read by the code.
    Can I set up a loop to be repeated x number of times to read in the subsequent lines? (I will have to replace [1] by[h], where h is a number that changes from 1-7.
    Also how do I read in the Species: as a series of characters (ie so the values first, second third etc are read not f, s, t etc)


    Please help I am so sorry but I have searched through the faq and previous threads as well as the web but have not found an answer to my problem.


    Many thanks,
    Nick

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int foo;
    while( (foo = fscanf( ...all this stuff... )) != EOF )
    {
        if( foo == numberofconversionsyouexpect )
        {
            ...do something...
        }
        else
        {
            ...incorrect number of conversions, handle your error somehow...
        }
    }
    Something like this perhaps?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Sorry, I just like playing with [fs]scanf.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       FILE *file = fopen("cond.inp", "r");
       if ( file )
       {
          int h;
          char conname[20];
          float conconc;
          double conrho, conmw, conprod, C, D, A, B;
          while ( fscanf(file, "Number: %d Species: %19s Concentration: %e "
                         "Density: %lf Molecular Weight: %lf Production Rate: %lf "
                         "Saturation Vapor Pressure%*[^:]: C=%lf D=%lf "
                         "Surface Tension%*[^:]: A=%lf B=%lf ",
                         &h, conname, &conconc, &conrho, &conmw, 
                         &conprod, &C, &D, &A, &B) == 10 )
          {
             printf("Number: %d\n"
                    "Species: %s\n"
                    "Concentration: %e\n"
                    "Density: %f\n"
                    "Molecular Weight: %f\n"
                    "Production Rate: %f\n"
                    "Saturation Vapor Pressure: C=%f D=%f\n"
                    "Surface Tension: A=%f B=%f\n\n",
                    h, conname, conconc, conrho, conmw, conprod, C, D, A, B);
          }
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    Number: 1
    Species: first
    Concentration: 1.000000e+14
    Density: 4.000000
    Molecular Weight: 1.000000
    Production Rate: 1.000000
    Saturation Vapor Pressure: C=1.000000 D=1.000000
    Surface Tension: A=1.000000 B=0.000000
    
    Number: 2
    Species: second
    Concentration: 1.000000e+14
    Density: 2.000000
    Molecular Weight: 1.000000
    Production Rate: 1.000000
    Saturation Vapor Pressure: C=1.000000 D=1.000000
    Surface Tension: A=1.000000 B=0.000000
    
    Number: 3
    Species: third
    Concentration: 1.000000e+14
    Density: 1.000000
    Molecular Weight: 1.000000
    Production Rate: 1.000000
    Saturation Vapor Pressure: C=1.000000 D=1.000000
    Surface Tension: A=1.000000 B=0.000000
    
    Number: 4
    Species: fourth
    Concentration: 1.000000e+14
    Density: 4.000000
    Molecular Weight: 1.000000
    Production Rate: 1.000000
    Saturation Vapor Pressure: C=1.000000 D=1.000000
    Surface Tension: A=1.000000 B=0.000000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    3

    Thanks Dave but a few questions

    Dave,
    That works great. However a few questions.

    1) If I want to save the parameters in a series of arrays how do I include it? tried aa[h]=conrho; ab[h]=conmw; etc
    but it gave me an error when run.
    2) Why do you have

    "Number: %d Species: %19s Concentration: %e "
    and
    "Density: %lf Molecular Weight: %lf Production Rate: %lf "
    on separate lines. I tried it with them all within one set of quotation marks and it only read the first line. How come what is so special about your use of quotation marks

    3) Finally I see that conname doesn't have a & infront of it Why? and if I want to have a name that is more than 1 character long how do I do it.

    Thanks alot for the first solution.
    All the best,
    Nick
    PS Sorry if the questions are stupid but it is amazing how some very small differences make such huge differences

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by NickHolmes
    1) If I want to save the parameters in a series of arrays how do I include it? tried aa[h]=conrho; ab[h]=conmw; etc
    but it gave me an error when run.
    I'd recommend a single array of a structure containing all common data.
    Code:
    #include <stdio.h>
    
    struct con
    {
       int    h;
       char   name[20];
       float  conc;
       double rho, mw, prod, C, D, A, B;
    };
    
    struct con mycon[5];
    
    int main(void)
    {
       FILE *file = fopen("cond.inp", "r");
       if ( file )
       {
          size_t i, j;
          for ( i = 0; i < sizeof mycon / sizeof *mycon; ++i )
          {
             if ( fscanf(file,
                         "Number: %d "
                         "Species: %19s "
                         "Concentration: %e "
                         "Density: %lf "
                         "Molecular Weight: %lf "
                         "Production Rate: %lf "
                         "Saturation Vapor Pressure%*[^:]: C=%lf D=%lf "
                         "Surface Tension%*[^:]: A=%lf B=%lf ",
                         &mycon[i].h,
                         mycon[i].name,
                         &mycon[i].conc,
                         &mycon[i].rho,
                         &mycon[i].mw,
                         &mycon[i].prod,
                         &mycon[i].C, &mycon[i].D,
                         &mycon[i].A, &mycon[i].B) != 10 )
             {
                break;
             }
          }
          fclose(file);
          for ( j = i, i = 0; i < j; ++i )
          {
             printf("Number: %d\n"
                    "Species: %s\n"
                    "Concentration: %e\n"
                    "Density: %f\n"
                    "Molecular Weight: %f\n"
                    "Production Rate: %f\n"
                    "Saturation Vapor Pressure: C=%f D=%f\n"
                    "Surface Tension: A=%f B=%f\n\n",
                    mycon[i].h,
                    mycon[i].name,
                    mycon[i].conc,
                    mycon[i].rho,
                    mycon[i].mw,
                    mycon[i].prod,
                    mycon[i].C, mycon[i].D,
                    mycon[i].A, mycon[i].B);
          }
       }
       return 0;
    }
    Quote Originally Posted by NickHolmes
    2) Why do you have

    "Number: %d Species: %19s Concentration: %e "
    and
    "Density: %lf Molecular Weight: %lf Production Rate: %lf "
    on separate lines. I tried it with them all within one set of quotation marks and it only read the first line. How come what is so special about your use of quotation marks
    Adjacent string literals are concatenated. So "Hello " "world" becomes "Hello world". Intervening whitespace (space, tab, newline, etc.) does not mess with this.

    Quote Originally Posted by NickHolmes
    3) Finally I see that conname doesn't have a & infront of it Why? and if I want to have a name that is more than 1 character long how do I do it.
    For a single character with %c you would use &conname. But for a string (%19s), you don't use &conname because an array name (conname) used in an expression decays into a pointer to the first element (&conname[0]). You don't want &conname for a string because the type is not correct.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    3

    Talking Thanks alot

    Dear Dave,
    You are a star. Thank you so very much.
    One last question when I try to print out the

    printf("\t%s",mycon[i].name);

    it only ever prints out the first value regardless of what i is and then when I repeat the statement

    ie printf("\t%s\t%s",mycon[i].name,mycon[i].name

    does the program print out the first 2 values.

    Also the first 2 values are null?

    Is there a simple way of saving mycon[1].name as some string, then mycon[2].name as another string, with the same name but a [x] type variable.
    I am sorry for being so thick. You have completely solve the read issues and things work great now.
    Thanks again
    Nick
    Last edited by NickHolmes; 05-29-2005 at 11:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM