input file problem

This is a discussion on input file problem within the C Programming forums, part of the General Programming Boards category; Hi everybody ! I'm a fresh newbie and I'm trying to read data from an input file which will always ...

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

    input file problem

    Hi everybody !
    I'm a fresh newbie and I'm trying to read data from an input file which will always look like this:

    TIME
    4 5.6 787 6 45
    COORDINATES
    4 78 6 22
    DATA
    1 2 3 4 5 6
    2 4 5 9 7 6
    3 6 5 4 7 8
    . . . .

    the headers (TIME, COORDINATS, DATA) are not important and I need to skip them with only knowing it is the right type of information.

    1.Is there a way to check that the first line really says: "TIME" ?
    2.how can I skip it using scanf (or whatever - this is the only command I know) ?
    3. I need to put the "TIME" and "COORDINATES" data into single variables and the "DATA" into structures, is it possible to stop the loop after reading the "DATA" header and start a new one for the structures input ?

    thanks a lot !!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    They're called state machines
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        enum { DO_INIT, DO_TIME, DO_COORDS, DO_DATA } state = DO_INIT;
        char buff[BUFSIZ];
        FILE *fp = fopen("words.txt", "r");
    
        while (fgets(buff, BUFSIZ, fp) != NULL) {
            switch (state) {
            case DO_INIT:
                printf("Init line=%s", buff);
                if (strncmp(buff, "TIME", 4) == 0) {
                    state = DO_TIME;
                }
                break;
            case DO_TIME:
                if (strncmp(buff, "COORDINATES", 11) == 0) {
                    state = DO_COORDS;
                } else {
                    printf("Time line=%s", buff);
                }
                break;
            case DO_COORDS:
                if (strncmp(buff, "DATA", 4) == 0) {
                    state = DO_DATA;
                } else {
                    printf("Coord line=%s", buff);
                }
                break;
            case DO_DATA:
                printf("Data line=%s", buff);
                break;
            }
        }
        fclose(fp);
        return 0;
    }
    Just replace the printf() calls with sscanf() converting the data as you see fit.
    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
    May 2005
    Posts
    6
    wow, a lot of new things...
    thanx salem.
    I read some info about the new things I didn't know (enum, switch, case) and I wanted to ask:

    1. what is: "BUFSIZ" and the meaning of the arguments here:
    Code:
    fgets(buff, BUFSIZ, fp
    2. in the "enum" line, what is the purpose of the variables ?
    do I need to give the values ?

    3. does "return 0" mean the "main" doesn't return any value ?

    4. (and the most important) how can I put the "DATA" values of each line into diffrent structures without knowing how many lines I can get (max is 50) ?

    thanks a LOTTT !

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > what is: "BUFSIZ"
    A constant in stdio.h

    > and the meaning of the arguments here:
    read your manual page on fgets()

    > 2. in the "enum" line, what is the purpose of the variables ?
    Well you have to name each state. It seemed better than STATE1, STATE2, STATE3
    > do I need to give the values ?
    No - enum numbers itself.

    > 3. does "return 0" mean the "main" doesn't return any value ?
    0 means success (as does EXIT_SUCCESS)
    EXIT_FAILURE means it failed.
    any other values depend on your environment as to what they actually mean to your OS.

    > 4. (and the most important) how can I put the "DATA" values of each line into diffrent structures
    How abount an array of structs
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 02:55 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. File input problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-14-2004, 05:54 AM

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