Question:
in c++ file handling, how can i insert some text in a text file without overwriting?
j t
2008-07-28 13:09:58 UTC
//when i used seekp(), it is overwriting the previous text
#include
void main()
{
char line[40];
ofstream outfile;
outfile.open("school.txt",ios::ate);
outfile.seekp(20);
cout<<"enter a line of text";
cin.getline(line,40);
outfile<outfile.close();
}
Five answers:
anonymous
2008-07-28 13:28:14 UTC
Unless the file object has been supplied with a method for this, which I'm not aware of, since I still primarily use C, you have to do it in a way that's consistent with the cluster organization of the filesystem. Unlike append, there is no corresponding open mode for insertion, because there is no straightforward way to insert. You have two choices:

1) You can read the file into memory, save the start address as a pointer, save the insertion point as a pointer, and allocate memory for the insertion string, then rewrite the file from start up to, but not including, the insertion point, then the string, then from the insertion point. You can't avoid having to overwrite, so you have to deal with it.

2) You can open a temp file for writing, read from the original and write to the temp up to the insertion point, write the insertion text into the temp file, and then read from the original and write to the temp file the remainder of the original. Then move the temp file over the original.
gameplayer_1706
2008-07-28 21:52:32 UTC
I agree with answer no. 2. No straight forward method.

steps..

1.U must first read all the remaining records from point of

insertion into another temp file.

2.Clear the records u copied.(not sure if u need this step)

3.Write ur riquired records.

4.Now copy the contents of the temp file back to original file

under append mode.



step 2 may not be needed cause the "end of file" mark

will be switched placed here, check this out.
VarmintHunter07
2008-07-28 13:23:17 UTC
Open with the file with the following mode flags:



std::ios::ate | std::ios::out | std::ios::in



=========================

Oops - I answered too quickly (without fully reading/understanding the question). I agree with the other answerer that there is no *straightforward* way to 'insert' without overwriting. The data at and following the point of insertion would need to be read into memory, updated with the insertion, and then rewritten to file.
anonymous
2016-12-10 15:29:01 UTC
you need to open the text fabric record and seek the URI's (or URLs) for the url you attempt to pass into. If none exist, then you may append the get right of entry to to the record. ----- Public classification URLHistory ' A holder for the record direction. inner maximum URLFile As String = FileIO.SpecialDirectories.MyDocuments & "try.txt" inner maximum function DoesURLExists(ByVal URL) As Boolean Dim lines() As String = record.ReadAllLines(URLFile) ' Open the text fabric record and seek for the URL. Debug.WriteLine("lines:" & lines.count type) for each Line In lines Debug.WriteLine(Line) If Line = URL Then return genuine end If next ' No URL replaced into chanced on. return fake end function Public Sub EnterURL(ByVal URL As String) ' Our text fabric author Dim urlWritter As StreamWriter ' Create the record if none exists. If record.Exists(URLFile) = fake Then Debug.WriteLine("springing up") urlWritter = record.CreateText(URLFile) urlWritter.close() end If ' We use the tactic above for the hunt. ' If it returns fails, then we use the author to pass into it. If DoesURLExists(URL) = fake Then Debug.WriteLine("Writing:" & URL) urlWritter = record.AppendText(URLFile) URLWritter.WriteLine(URL) URLWritter.Flush() URLWritter.close() end If end Sub end classification
anonymous
2008-07-28 20:33:31 UTC
use outfile.open("school.txt",ios::app);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...