Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert a line in a text file using fstream

Posted on 2001-07-12
6
Medium Priority
?
973 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
Technology Partners: 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 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