Solved

Insert a line in a text file using fstream

Posted on 2001-07-12
6
885 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
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now