Thread: Reading from File

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    Reading from File

    I am trying to figure out how to read formatted input from a file. If this were C++, I would use getline to read a line of the file and then pass that line to a stringstream. But this is the first time I am doing file I/O in C and I am stuck.

    For example, the file would be in the form of:

    Some Picture
    4 3
    128 128
    jpg
    jpeg
    12 3 8 5 4 11 7 1 9 6 10 2

    The first line is a character string.
    the next two lines are integers separated by a tab.
    The next two lines are strings.
    The last line are integers separated by spaces.

    I have the following so far, but it looks like it only reads one string from the line at a time and if it encounters the integers, it may crash since it was expecting a string.:

    Code:
    FILE  *INFILE;
    gchar str [80];
         
    INFILE = fopen (appState->fileName, "r");
        
         while(INFILE)
         {
              fscanf (INFILE, "%s", str);
    Is there a way to do something like the istringstream? Thanks.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I would use getline to read a line of the file and then pass that line to a stringstream
    You could still use the same tech. Use fgets in C. And perhaps to parse the string use sscanf function.

    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by ssharish2005 View Post
    You could still use the same tech. Use fgets in C. And perhaps to parse the string use sscanf function.

    ssharish
    Thanks for your help, that did it.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    One of the lines in the file will always contain only 12 integers. How can I loop the following code to insert each integer read into an array.

    Code:
    sscanf (line, "%d %d %d %d %d %d %d %d %d %d %d %d", &img1, &img2,  &img3,  &img4, 
                                                                                                &img5, &img6,  &img7,  &img8,     
                                                                                                &img9, &img10, &img11, &img12);
    Again, in C++ I would have written something like:

    Code:
    while(iss >> anInteger)
    {
      intVector.push_back(anInteger);
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I am now writing another program, but have a similar question about sscanf. I want to read a line from a file in the form:

    lastname firstname, 123 Fake ST, 1 , Somewhere, US, 0000

    My problem is that sscanf ignores and reads until whitespace is reached, so if I want a string with "lastname firstname" it looks like I will have to have various tmp strings and concatenate them using strcat.

    Is there a way to ignore the whitespace and read up to a comma instead? Thanks.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    "%[^,]"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I have been trying several different things, but have been unable to extract the data from the file correctly. I printed the return value of sscanf, and it read only 1 string from the line. So the format specifier %[^,] reads a string up to a comma, but it seems that it does not continue reading after it encounters a comma. Could someone provide a hint on how to continue reading the entire comma separated line? Thanks.

    Code:
        FILE  *INFILE;
        
        INFILE = fopen (application->fileName, "r");
     
        while( fgets(line, 200, INFILE) != NULL)  
       {
                      sscanf (line, "%[^,] %[^,] %[^,] %[^,] %[^,] %[^,] %[^,]  %[^,] %[^,] %[^,] %[^,]" ,  tmpName,
            										                                                                tmpGroup,
             										                                                                tmpStreet,
                
                                                 //.......................

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I found a solution and will post in case someone else encounters the same issue.

    Add a %*c to read but not store the comma in between the fields.

    Code:
     sscanf (line, "%[^,] %*c %[^,]

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you know that there is comma that you need to skip - just write it
    sscanf (line, "%[^,],%[^,]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM