fstream

This is a discussion on fstream within the C++ Programming forums, part of the General Programming Boards category; Hi everybody. Does anybody know of a way to insert a line of text in the begging of the textfile ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    fstream

    Hi everybody. Does anybody know of a way to insert a line of text in the begging of the textfile without having to read an entire file in a loop? Preferably using ifstream? Thank you for the replies.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You cannot do what I think you want with fstream. I'm not sure you could even do it even with platform specific file i/o functions.

    However, you can do it without using a loop. Just read the entire contents of the file into a string via a stringstream, then output the new first line followed by that string.
    Code:
    ostringstream ostr;
    ostr << in_file.rd_buf();
    out_file << "this is the new first line.\n" << ostr.str();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 10:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 11:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 04:36 PM

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