Solved

Insert a line in a text file using fstream

Posted on 2001-07-12
6
923 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 108
Issues with C++ Class 19 108
c++, dynamic object by json 1 60
How to convert Structure to vector of byte and vice versa 5 31
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

733 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