read-lines in two stages

This is a discussion on read-lines in two stages within the C Programming forums, part of the General Programming Boards category; hello, i want to read and store data from lines of a file which has the following structure: they start ...

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

    read-lines in two stages

    hello, i want to read and store data from lines of a file which has the following structure:
    they start with a character which is either c or s, if it is c three integers follow, if it is s two integers follow, how can i do that, i should read the first character, and then the rest of the line accordingly.
    Thank you very much,
    plutonas

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    fgets and sscanf for exsample, there is a lot of samples on the board
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    I can't find any examples in the board, perhaps i'm searching wrong. Could you be so kind to provide me with some links?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    > i should read the first character, and then the rest of the line accordingly.
    Yeah. Basically all you have to do is read the whole line, check the first character, and then pass the according format string to sscanf the rest of the line. Give it a shot. I'll help you get it right if you have problems.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    I think i got it:

    Code:
     pT = fopen(filename,"r");
      fgets(str, 32,pT);
      sscanf(str,"%d", &lines);
      for (i=0; i<lines; i++){
        fgets(str,32,pT);
        sscanf(str,"%c %d %d %d %d %d %d", &c, &i1, &i2, &i3, &i4, &b1, &b2);
        if (c=='s')
          printf("%d %d %d %d\n", i1, i2, i3, i4);
        else
          printf("%d %d %d %d %d %d\n", i1, i2, i3, i4, b1, b2);
      }
      fclose(pT);
    now i should put the values in structures, but anyway, thats better than i/o

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Mmm, maybe, but you could use a little bit more error checking. I was going to recommend something like this.
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      int that[3];
      int other[2];
      const char *const lines[] =
      {
        "c4 4 5", "s8 0", "s 34\t5", "c 0 6",
        "a 4 5 9", "c 45 7 7", "s33", "\tc 4 4 4",
      };
    
      size_t idx;
      for( idx = 0; idx < sizeof lines / sizeof lines[0]; idx++ )
      {
        /*
         * If the first character is c, 3 integers follow; if it is s then two integers follow.
         * Right now the test is case sensitive; to make it case insensitive, use tolower()
         */
        if( lines[idx][0] == 'c'  /* check initial */ &&
          sscanf( &lines[idx][1], /* skip initial */ "%d%d%d", &that[0], &that[1], &that[2] ) == 3 )
        {
          printf( "\"%s\" is OK.\n", lines[idx] );
        }
        /*
         * Do basically the same for the other case; you don't even have to use separate
         * storage.
         */
        else if( lines[idx][0] == 's' &&
          sscanf( &lines[idx][1], "%d%d", &other[0], &other[1] ) == 2 )
        {
          printf( "\"%s\" is OK.\n", lines[idx] );
        }
        else
        {
          printf( "ERROR >  Not a valid form.\n" );
        }
      }
    
      return 0;
    }
    As you would expect, about half the test cases work. An approach like this is easily adapted to working with files.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    well the files are supposed to be correct, so no error checking is required... :-)

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by plutonas View Post
    well the files are supposed to be correct, so no error checking is required... :-)
    And if for some reason your OS decides not to let you open the file? Or the file gets corrupted somehow...?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    The project says: "You can take for granted that the files are correct, so you don't have to do any error checking". So i think i will not do any. Of course if it was a real-world programm, of course i would do error checking, and probably a lot more things. As for the OS, no problem, i run neither windows nor ubuntu!
    (the last one was a joke)
    Thanks very much, i'll ask more if new problems appear, it still needs a lot of work...
    plutonas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 06:18 PM
  2. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  4. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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