traversing text file

This is a discussion on traversing text file within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if anyone has any ideas on how I can establish multiple traversals through an opened text ...

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

    traversing text file

    Hi,

    I was wondering if anyone has any ideas on how I can establish multiple traversals through an opened text file. That is, can I "getline" at one point in the file, move to another point in the file and get another line, and then go back to the original point? Or do I have to go through the file line by line.

    (Hope that made sense)
    Thanks for any help!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can I "getline" at one point in the file, move to another point in
    >the file and get another line, and then go back to the original point?
    Yes. Look into the tellg and seekg member functions of std::ifstream. But this is a subtle area of the iostream library, so it's easy to break something without knowing it.

    >Or do I have to go through the file line by line.
    It would be safer and easier to just store the lines in memory, such as a vector, and work with that instead of trying to seek through the file.
    My best code is written with the delete key.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Or do I have to go through the file line by line.

    Well, you would basically have to go through the whole file and map out where the line breaks are, and then jump around with seekg to those points. It just seems easier to read the whole file while you're doing this into a more easily managed vector of lines.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Thanks for your help. I'm relatively new to C++ so I was wondering if you could give me some further help. Here's my little dilemma: I have a text file containing a bunch of contacts (addresses, phone numbers, etc.). They are arranged one after another in 'blocks' so that there is at least one blank space between each contact. The problem is, many of them are duplicates. This text file is over 1800 pages so I would like to write a program that finds these duplicates and deletes them (instead of manually going through them myself). Any ideas what approach I might take? Thanks!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Insert them into an std::set. Duplicates will be removed automagically and you don't have to fiddle with any complicated or inefficient logic.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Thanks. Where can I learn more about std::set?

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    What does the text file look like? Like this?

    Code:
    Name: Batman
    Address: Gotham City, Batcave
    Phone Number: 123456789
    
    Name: Spiderman
    Address: NYC, USA
    Phone Number: 987654321
    You could read each contact into a Contact object, then insert those Contact objects into a std::set (as Prelude said). Google has plenty of infomration on std::set. Here's the SGI reference page.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Thanks. Yeah, that's kind of how the text file looks, except the contacts are not all of the same format. Some have extra fields, others don't. Would this pose a problem? Also, how would I read each contact into a Contact object?

  9. #9
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by AML25
    Also, how would I read each contact into a Contact object?
    If you're sure that each Contact is separated by an empty line, then I'd do something like this

    1. Create a std::map<string,string>.
    2. If there's input, read and put everything you see in a std::string until you reach an empty line. If there's no input, goto 6.
    3. Parse what you've read and find the name (e.g. batman)
    4. If the name already exists in the map, go back to 2. Else add the contactString to the map with the name as the key. E.g.
    Code:
    m["batman"]=contactString;
    5. Go back to 2.
    6. stop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  3. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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