Reading a file again

This is a discussion on Reading a file again within the C++ Programming forums, part of the General Programming Boards category; Hi, how can I start back from the beginning of a file after I have already read through it to ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Reading a file again

    Hi, how can I start back from the beginning of a file after I have already read through it to read it again? Right now I have read through each line and closed it and re-opened it with a different pointer name.I need to read from it a 3rd time and another 3rd name for the file would be ridiculous,

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    try
    Code:
    std::ifstream fin("file.txt");
    // Read the stuff in
    fin.close();
    fin.clear();
    fin.open("file.txt");
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are talking about a "pointer name" so I think you are talking about opening the file with th c-function fopen().
    You dont even have to close the file before reading it again. just use
    Code:
    FILE * f = fopen(...);
    // read the complete file
    fclear(f);  // reset eof 
    rewind(f); 
    // read again
    Kurt

  4. #4
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    When the file is closed and the opened again won't it start reading it from the beginning?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by 182
    When the file is closed and the opened again won't it start reading it from the beginning?
    Yes it will start reading from the beginning.
    Kurt

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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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