Thread: Reading text files

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    6

    Reading text files

    Pls, help me: how can i do this:

    I read a text file line-by-line using ifstream.getline();
    How can i reposition the pointer so after i read for example 3 lines to start reading from the first line.

    Thank you

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    - open the file again
    - store all the lines in an array, accses it after reading
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Quote Originally Posted by ElastoManiac
    - open the file again
    - store all the lines in an array, accses it after reading
    Thank you for the reply but this is not my point, because i have thousands of lines, i don't want to store all the lines in memory. I wonder how can i close the stream or the file and open it again for processing lines from the begining.

    any ideea?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What about seekg()?
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 - create the index
    - use tellg() to find out where you are in the file
    - read a line from the file
    - loop until end of file.

    Step 2 - use the index
    - use seekg() to goto a previous tellg() result
    - read the line
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Quote Originally Posted by Salem
    Step 1 - create the index
    - use tellg() to find out where you are in the file
    - read a line from the file
    - loop until end of file.

    Step 2 - use the index
    - use seekg() to goto a previous tellg() result
    - read the line
    i did like this and didn't works:
    Code:
                   // ...
                   char s[200];
    
    		ifstream fin;
    		fin.open("C:\\in.txt");
    
                                   
                                    do {
                                       fin.seekg(0, ios::big);
                                       // read a string from the keyboard...
    
                                        while(fin.getline(str, 200)) {
                                                // if the string is found then break;
                                        }
    
    
                                     }while(true); // suppose reading at infinity

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fin.seekg(0, ios::big);
    I thought it was beg, not big?

    Also, it needs to be INSIDE the loop with the getline.

    Also, you need to store each answer tellg() returns.

    What doesn't work?
    - it doesn't compile
    - it always reads the first line
    - it crashes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Quote Originally Posted by Salem
    > fin.seekg(0, ios::big);
    I thought it was beg, not big?

    Also, it needs to be INSIDE the loop with the getline.

    Also, you need to store each answer tellg() returns.

    What doesn't work?
    - it doesn't compile
    - it always reads the first line
    - it crashes
    Sorry: was beg, not big.
    The positioning don't work simply with seekg(...).
    I will try to retrive with tellg...

    Thank you very much for the reply.
    Best regards!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  2. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM