Thread: go to next line in .txt file

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    go to next line in .txt file

    Hi, I made a writing to a .txt file program. It works fine, but, when I put it my .txt it just replaces that line of the texts in the .txt file. I was wanting know is there a way to go to the nexts line to write the new texts? Here my code:
    Code:
    //<******files and headers are right just my code
        ofstream myfile;
        myfile.open ("learnthis.txt");
        getline(cin,learn);
        learn=say+learn;
        myfile << learn;
        myfile.close();}}}

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    There is probably a proper way to do this, but just off the top of my head, why don't you read the contents of the text file before writing? Then you could just append what you want to add to the file, and then replace the file with the new string.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you simply want to add to the end of a file, you can open it with the append flag:
    Code:
    std::ofstream out("somefile.txt",std::ios::app);
    Otherwise, I think you will have to search through the file for the correct position, and use seekp to set the write position (no pun intended)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok I see what your saying joeprogrammer. You mean something like, openfile, save as a string, add strings togather, then that be the new string? I will try that, I didnt think of that.

    JaWiB: LOL, I know you dont mean it as a pun but, I never use seekp. Could you show a small way to use it?

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    AFAIK you can't just insert stuff in the middle of the file, you have to manually write over everything. Here's a little example:
    Code:
    /* test.txt:
    Line1
    Line2
    Line4
    Line5
    */
    #include <fstream>
    #include <string>
    
    int main ()
    {
      std::fstream file("test.txt",std::ios::in|std::ios::out);
      std::string line;
      std::string buffer;
      std::streampos linepos;
      bool found = false;
      while (file>>line)
      {
        if (line=="Line2")
        {
          file.ignore();//ignore the endline character
          linepos = file.tellg();//get this file read position
          found=true;
        }
        else if (found)
        {
          buffer+=line+'\n'; //store what was after that line so we can rewrite it later
        }
      }
      //we've hit the end of the file, so eofbit is set
      file.clear(); //clear all the state flags
      file.seekp(linepos); //find the pos where we want to insert
      file<<"Line3\n"<<buffer; //write the line, then the rest of the text
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Are you able to pass the '\n' bit, (I forget the word for it) to the text file? And by doing so, start on a new line?
    Oh my goodness.

  7. #7
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    yes, and it is called a newline character
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM