#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Canada
    Posts
    1
    Rep Power
    0

    Question read from a formated data file


    Hello...
    I need to read from a data file and then do use the data for computations. I cannot figure out a way to extract the data. This is what the file looks like(of course, the comments are not in the file):

    AB111 //its the ID
    34
    12 22 4.6
    33 67 2.3
    .
    -1 -1 -1 //end of data block
    AB567 //next ID
    45
    14 44 6.8
    .
    -1 -1 -1 //marks end of data block
    .
    .

    I need to read each number related to an ID, even the three numbers in one line. Next, for each ID, I use the numbers to computer other stuff. The first two lines for each data block is going to contain the same info(ID# and some integer). The next lines will have 3 numbers in each.

    Basically I cannot figure out a way to capture the data, esp. the 3 numbers that are on the same line.
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Ok, you can read lines as a whole using fgets(), then you can use strtod() to get the actual floating-point values. The easiest way (IMO) to communicate the converted values and the amount of values is by using a prototype like:

    Code:
    int scan_line (char *lline, int *num_values, double values[]);
    with *num_values being the amount of numbers converted and returned in values[]. Obviously values[] has to be large enough to hold three values.

    Hope this helps.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Why not just use fscanf() as in:
    Code:
    #include <stdio.h>
    int main(void) {
      FILE *fp;
    
      char sID[10];
      int num1, num2, num3, num4;
      char dummy[2];
    
    
      if ((fp = fopen("test.txt", "r")) == NULL)
        return 1;
    
      fscanf(fp, "%s", sID); /* Read the ID */
      fscanf(fp, "%d", &num1); /* Read the next number */
      fscanf(fp, "%d %d %d", &num2, &num3, &num4); /*Read 3 numbers */
      fclose(fp);
    
      printf("We have %s and numbers %d %d %d %d\n", sID, num1, num2, num3, num4);
    
      return 0;
    }
    This way you don't have to convert the values separately. Hope this helps!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12

    Re: read from a formated data file


    Originally posted by CplusQ
    Hello...
    I need to read from a data file and then do use the data for computations. I cannot figure out a way to extract the data. This is what the file looks like(of course, the comments are not in the file):

    AB111 //its the ID
    34
    12 22 4.6
    33 67 2.3
    .
    -1 -1 -1 //end of data block
    CplusQ,

    The function to do this is pretty simple. The data structure necessary to maintain all of that data is less so. You have two options:

    1) store it in a linked list - if you don't know what a linked list is, grab a copy of "Mastering Data Structures and Algorithms in C" from O'Rielly & Associates, which will provide both explanation and implementation.

    2) Store them in a two-dimensional array. This is harier than the linked lists, because you'll need to determine the size of the array and allocate it dynamically. I wouldn't do this unless the ability to index into the list of numbers is essential to your calculations.

    The general solution for the second route is to read the entire data file into memory, and store it as a big string. The following code will do that for you:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>

    ...

    struct stat sb;
    FILE* fdata;
    char* data;

    if (stat("datafile.dat", &sb)) {
    perror("stat");
    return 0;
    }
    data = (char*)calloc(1, sb.st_size + 1);
    fdata = fopen("datafile.dat", "r"):
    fread((void*)data, sb.st_size, 1, fdata);
    fclose(fdata);



    Now that you have all of this data in memory (in the string data), you can use character pointers as bookmarks to remember where a record started. You then read to the end of the record and keep count of how many rows of decimals you read. Then allocate space for them, go back to your bookmark, and start again.

    It's going to take some work and debugging on your part, but the basic routine is pretty simple.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo