reading values from a file

This is a discussion on reading values from a file within the C Programming forums, part of the General Programming Boards category; There is a file which has data in the way shown below. Each row is sort of a record. For ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    24

    reading values from a file

    There is a file which has data in the way shown below.
    Each row is sort of a record.
    For each record i will be needing values in the first column(+/- 1) and the other floating
    point values in the row
    eg:.23232
    (not the colon':' and the serial numbers)
    (The number of fields is same in each row)
    The source is in c.The way i had in mind was using a structure
    with int char double fields and then using fscanf to read them as it is
    and then extracting the requisite values.
    The problem is since the data in all rows may not be spaced uniformly in
    all rows(not a possibility but since data is huge cannot be predicted)
    it is becoming difficult to move the file pointer in same way in all rows.
    Can someone suggest a simple and computationally inexpensive way of
    storing the values?

    FILE

    Code:
    1 1:.2345 2:.3454 3:.238909  ........            10,000:.4329407
    -1 1:.1201890 2:.121211 .............               10,000:.3123123
    -1 1:.12323 2:.312312.......                                10,000:.312081290
    .
    .
    .
    .
    .
    -1  1:.3213123 2:.321312                                     10,000:.31231232

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Are you suggesting that you have up to 10000 id:value pairs on each row of the table?

    Do the id parts really have comma in the thousands ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    The number of entries depends on the file(usually of the order thousands)
    but the essential format is the same.
    Thus the id:value pairs are in thousands.
    Sorry about the comma part, there are no commas in the thousands,
    thanks for pointing out.
    Also the total number of rows generally does not exceed 100.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Try this
    Code:
    #include<stdio.h>
    int main ( ) {
        FILE   *fp;
    
        fp = fopen("foo.txt","r");
        while ( 1 ) {
            int     id, result;
            double  val;
            result = fscanf( fp, "%d:%lf", &id, &val );
            if ( result == 1 ) {
                printf("ID: ID=%d\n", id );
            } else
            if ( result == 2 ) {
                printf("Pair: Value=%f\n", val );
            } else {
                /* almost certainly EOF, but in any case we're done */
                break;
            }
        }
        fclose(fp);    
    
    	return 0;
    }
    
    $ cat foo.txt && ./a.exe
    1 1:.2345 2:.3454 3:.238909 10000:.4329407
    -1 1:.1201890 2:.121211 10000:.3123123
    
    ID: ID=1
    Pair: Value=0.234500
    Pair: Value=0.345400
    Pair: Value=0.238909
    Pair: Value=0.432941
    ID: ID=-1
    Pair: Value=0.120189
    Pair: Value=0.121211
    Pair: Value=0.312312
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    Thanks !!
    That was clever.. using fscanf's return value(did'nt remember []).
    It is working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 06:15 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21