Thread: How should I read this file (i/o question)

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    How should I read this file? (i/o question)

    Well I studied and knew c well about three years ago, since then, java has rotted my brain and I need some help.

    What I'm doing is opening a file (see below) and writing 3 coordinates into an array. The only problem is there are 'j' or 'J' charecters that seperate some of the coordinates. I need to know how to read the file while ignoring the j charecters.

    Code:
    J
       366 -1722   583
       366   356  1783
       866   789  1033
       866 -1289  -167
       366 -1722   583
    J
      -500 -1472   150
         0 -1039  -600
         0  1039   600
      -500   606  1350
      -500 -1472   150
    J
       366   356  1783
      -500   606  1350
         0  1039   600
       866   789  1033
       366   356  1783
    My main problem is that I have 4 files to read and there are not always 5 sets of coordinates in between each 'j' so Im unsure how to ignore the j.

    Any ideas of how to read this type of file?
    Last edited by mmattax; 01-16-2006 at 03:13 PM.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Lots of ways to do this.

    If you use fgets() to read a line into a buffer, check the first character to see if it's 'j' or 'J', and if it is, ignore the line. This is probably the easiest.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    LOOP:

    if ( returnValueFromFile != 'j' )
    SaveCoordinates;

    ?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    I'll try those suggestions and post any problems, thanks.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Ok, I am now using fgets(), and havea condition to ignore the 'j' charecters.

    But if it doesn't read a 'j' I am having trouble isolating the 3 int values on that line, is there an easy way to read those?

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    it does read 'J' and only then you check if it's a 'J' and ignore it, like this:
    Code:
    char buf[64];
    while (fgets(buf, sizeof buf, fp) != NULL)
    {
      if (buf[0] == 'J' || buf[0] == 'j')
        continue;
      else
      /* do stuff */
    }
    :wq

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    He said he had that and needed to isolate the int values viaxd. Perhaps sscanf(..) is appropriate here?

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Quote Originally Posted by viaxd
    it does read 'J' and only then you check if it's a 'J' and ignore it, like this:
    Code:
    char buf[64];
    while (fgets(buf, sizeof buf, fp) != NULL)
    {
      if (buf[0] == 'J' || buf[0] == 'j')
        continue;
      else
      /* do stuff */
    }

    I had all of this. I just do't know how to read the numbers now because the Strings look like " 366 -1722 583" so im unsure how to isolate the digits and not the spaces.

    any ideas?

    Thanks for your help.

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Code:
    long val1, val2, val3;
    
    ...
    if (sscanf(buf, "%d %d %d", &val1, &val2, &val3) < 3)
    {
         /* didn't get all three values ... */
         ... handle error condition ...
    }
    else
    {
        ... do something with val1, val2, and val3 ...
    }
    Last edited by filker0; 01-16-2006 at 04:35 PM. Reason: was missing a '&'
    Insert obnoxious but pithy remark here

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thanks, I had no clue that sscanf exisited. Thats a pretty sweet function!

    Thanks for all the help guys!

  11. #11
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    shudn't it be fscanf? I'm prolly wrong here, I just don't happen to know sscanf personally...

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you could use fscanf, a better pair is fgets to read the line, and then sscanf to extract the data. While it may seem like more work, it's usually better, because now you have (guarinteed, save for some EOF or error with your file getting deleted while you're using it, etc...) for sure the entire line in a buffer. Where as if you just use fscanf, and it reads part of its conversions, but not all, you have to wonder where exactly in your file you are now.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by filker0
    Code:
    long val1, val2, val3;
    
    ...
    if (sscanf(buf, "%d %d %d", &val1, &val2, &val3) < 3)
    This either needs to be int, or the directives should be %ld.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Dave_Sinkula
    This either needs to be int, or the directives should be %ld.
    You're quite right. My bad. I had originally typed it as %ld, then went back and amended it to %d to make it less a complicated example to understand, however I forgot to fix the declaration at the top.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 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. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM