Like Tree1Likes

Extracting Information from Files

This is a discussion on Extracting Information from Files within the C Programming forums, part of the General Programming Boards category; Hello, What is the best, and easiest way to extract information from Files? For example: I open afile.txt afile.txt contains: ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    26

    Extracting Information from Files

    Hello,

    What is the best, and easiest way to extract information from Files?

    For example:

    I open afile.txt

    afile.txt contains:

    /*
    City Distance1 Distance2 AvgDistance

    Miami 50 60
    Montreal 90 65
    Vancouver 87 63

    */

    In that exact spacing. How can I extract the strings and numbers and use it?
    I know it's a simple solution, but I can't seem to work it out. I don't mind if it I have to store each into it's own variable. As long as you can explain your answer, it would HELP a lot more than just posting a solution.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do the same thing this guy needs to do: NEED GUIDANCE: Loading String from a .txt/.dat file into Arrays


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pztar View Post
    Hello,

    What is the best, and easiest way to extract information from Files?

    For example:

    I open afile.txt

    afile.txt contains:

    /*
    City Distance1 Distance2 AvgDistance

    Miami 50 60
    Montreal 90 65
    Vancouver 87 63

    */

    In that exact spacing. How can I extract the strings and numbers and use it?
    I know it's a simple solution, but I can't seem to work it out. I don't mind if it I have to store each into it's own variable. As long as you can explain your answer, it would HELP a lot more than just posting a solution.

    Thanks
    Take a look at the two functions fgets() and sscanf() in your C library documentation.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    26
    I still don't know how to do it. I retrieve the information with fgets() that works fine for me.

    But I can't get the information that I want with sscanf(). I'm not sure how to do it..

    I've searched google, and I can't find what I'm looking for. I found something where the example used a sentence and that seems simple. But I don't know how to apply it to my situation.

    I know I may be asking too much, but I just want to learn how to do it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sscanf works the same as scanf. How would you use scanf to solve your problem?

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    26
    Well I sort of got it figured out.. But fgets is reading the whitespace and then just stopping. How can I make it skip to the next line?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    fgets most assuredly does not skip white space. It only stops at the end of the line.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Something like this:
    Code:
    fgets(buffer, BUF_SIZE, filep);
    sscanf(buffer, "%s %d %d", city, &dist1, &dist2);

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pztar View Post
    I still don't know how to do it. I retrieve the information with fgets() that works fine for me.

    But I can't get the information that I want with sscanf(). I'm not sure how to do it..

    I've searched google, and I can't find what I'm looking for. I found something where the example used a sentence and that seems simple. But I don't know how to apply it to my situation.

    I know I may be asking too much, but I just want to learn how to do it.
    Cygnus C Support Library, Full - scanf

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    26
    Quote Originally Posted by nonoob View Post
    Something like this:
    Code:
    fgets(buffer, BUF_SIZE, filep);
    sscanf(buffer, "%s %d %d", city, &dist1, &dist2);

    That's what I'm trying to do but it won't work. When I don't loop it, it returns 0 all the time. When I do, it returns only the values of the last line

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Perhaps you could post the simplest version of your code that demonstrates the problem?
    quzah likes this.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Pztar View Post
    That's what I'm trying to do but it won't work. When I don't loop it, it returns 0 all the time. When I do, it returns only the values of the last line
    It sounds like when you are looping you are just writing to the same spot:
    Code:
    while( ... )
        sscanf( "%d", &putithere );
    printf( "oh noes, putithere only contains the last entry: %d\n", putithere );

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

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    26
    Quote Originally Posted by anduril462 View Post
    Perhaps you could post the simplest version of your code that demonstrates the problem?
    It's incorrect, I know. But I'm a beginner and I'm just trying to work it out.

    Code:
    while (fgets(rString, 100, fp))
    		{
    			
    			sscanf(rString, "%s %d %d %d", iD, &R1, &R2, &RB);
    			printf("%s %d %d %d", iD, R1, R2, RB);
    			
    		}
    The main problem I'm getting now is, it's scanning the whitespace and returning odd values, and it's scanning the first set of strings. How can I get it to skip it? Thanks

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    26
    Quote Originally Posted by anduril462 View Post
    Perhaps you could post the simplest version of your code that demonstrates the problem?
    It's incorrect, I know. But I'm a beginner and I'm just trying to work it out.

    Code:
    while (fgets(rString, 100, fp))
    		{
    			
    			sscanf(rString, "%s %d %d %d", iD, &R1, &R2, &RB);
    			printf("%s %d %d %d", iD, R1, R2, RB);
    			
    		}
    The main problem I'm getting now is, it's scanning the whitespace and returning odd values, and it's scanning the first set of strings. How can I get it to skip it? Thanks

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are still going to be overwriting the same spot every time:
    Code:
    			sscanf(rString, "%s %d %d %d", iD, &R1, &R2, &RB);
    Every pass through the loop will replace whatever value those variables have with new ones. If you need to keep track of multiples of each, then you probably want an array or something similar.

    If you want to skip just a single line, call fgets once outside the loop first:
    Code:
    if( fgets( rString, 100, fp ) != NULL ) /* ignore first line */
    {
        while( fgets( ... ) /* for each remaining line... */
        {
            ... do stuff
        }
    }
    else /* first line skip failed for some reason */
    {
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting Files
    By man_of _my_word in forum C Programming
    Replies: 8
    Last Post: 12-23-2010, 01:31 PM
  2. Storing information created files.
    By GCNDoug in forum C Programming
    Replies: 12
    Last Post: 05-07-2007, 04:18 PM
  3. extracting value
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-13-2006, 03:09 PM
  4. Extracting information from a website?
    By gflores in forum Tech Board
    Replies: 2
    Last Post: 11-14-2005, 08:31 AM
  5. extracting files from a rar/zip
    By jverkoey in forum Windows Programming
    Replies: 2
    Last Post: 03-28-2003, 08:55 AM

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