?
Solved

Insert a line in a text file using fstream

Posted on 2001-07-12
6
Medium Priority
?
947 Views
Last Modified: 2008-02-01
Hi,

I'm new to C++. I have an application that has to insert a line in a text file (if possible without creating another file).

I have to use STL and fstream.

Any ideas how to do it?

Thanks,
Sorin
0
Comment
Question by:sorinv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 800 total points
ID: 6278332
It is impossible to insert information into a file.  you can write over information in a file, but you cannot insert new information.   i..e you can write out new information, but that new information writes on top of existig information, it does not insert the information by making room for it in the middle of the existing information.

There are two main ways to accomplish this.  

One is to copy data from the first file to a 2nd file until you reach the insert point.  then insert the new information (write it to the 2nd file), then copy the information remaining in the source file.  Then you copy all the inforamtion from the 2nd file back to the first.

The other way is to read the fle into memory, change it in memory, then write it back to disk.

example follows.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6278337
You can use std::vector<string> to read each line in the text file.  While you're reading the line into the std::vector<string>, you can insert a line.
Then you can write to the text file, but writeing the std::vector<string> to it.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6278349
When you read it, use the global getline function to read the text file line by line.
Something like the following:
     vector<string> lines;
     fstream in("test2.cpp");
     while (!in.eof())
     {
          string line;
          getline(in,line);
          lines.push_back(line);
     }
     in.close();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 6278354
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
   vector<string> FilDat;
   fstream Fil("filename.txt");

   // Read in all the data
  while (!Fil.eof()))
  {
     string Lin;

     getline(Fil,Lin);
     FilDat.push_back(Lin);
  }

  // Insert a new line after the 3rd line.
  FilDat.insert(FilDat.begin()+2,"This is a new line");

  // Write out the data.
  const int LinCnt = FilDat.size();  

  Fil.seekp(0);
  for (int i = 0; i < LinCnt; ++i)
      Fil << FilDat[i] << endl;

   return 0;
}
0
 

Author Comment

by:sorinv
ID: 6279656
Thanks. I thought it would be this way, but I hopped you guys would have come with a better solution. Thanks nayway for clarifying the things for me.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6280224
Every word processor in the world works in one of these ways.   Most are now memory based, readig all the file in and manipulating it in memory, but when memory was tight they sed to be mostly file based.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question