Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appenging to the start of a file.

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
I have a text file that I want to append to the start of. I know how to write to the beginning of the text file but not overwrite the information that is already there.

Any one have any idea how to do this?

Smilee
 
You should open file in write mode and all the content will be automatically removed. Ion Filipski
1c.bmp


filipski@excite.com
 
I have the following to open a file and write to the top. It seems to overwrite the data there. How would I change this function to simply make it append to the top?

WritetoTop(CString szErrorFile, CString szOutputFile)
{

if(!CopyFile( szErrorFile, szOutputFile, false))
{
AfxMessageBox("Error: Could not copy " + szErrorFile + "to " + szOutputFile);
return false;
}
else
{
PrintMessageToFile(&quot;</ROOT>&quot;, szOutputFile);
fstream File(szOutputFile,ios::eek:ut | ios::in /*| ios::binary*/ );
File.seekg(ios::beg);
File.write(&quot;SomeText&quot;,sizeof(&quot;SomeText&quot;));
File.close();
}
return true;
}
 
You can use ofstream instead of fstream.
ofstream xxx(&quot;filename&quot;); will delete all the content of the filename and will open it in write mode. Further you can xxx.write(anythingyouwant...) or xxx<<anythingyouwant Ion Filipski
1c.bmp


filipski@excite.com
 
I *do not* want to overwrite any of the data or delete the file.

I have a file that already contains some data and I want to add a line at the top of the file without overwriting anything. Using ofstream will not help me to do this.

I am now using:

WritetoTop(CString szErrorFile, CString szOutputFile)
{

if(!CopyFile( szErrorFile, szOutputFile, false))
{
AfxMessageBox(&quot;Error: Could not copy &quot; + szErrorFile + &quot;to &quot; + szOutputFile);
return false;
}
else
{
PrintMessageToFile(&quot;</ROOT>&quot;, szOutputFile);
ofstream File(szOutputFile,ios::eek:ut | ios::in /*| ios::binary*/ );
File.seekp(ios::beg);
File.write(&quot;SomeText&quot;,sizeof(&quot;SomeText&quot;));
File.close();
}
return true;
}

But is still doesn't work.

Smilee
 
Tiger:

I understand what you are trying to do , is the file too large?

Try the following:

1. Open the file
2. Read the contents to memory and close the file
3. Open the file using ofstream
4. Write the lines you want to add.
5. Write the rest of the file that is in memory
6. Close the file

Regards,

Ricardo
 
Tiger, do you want to insert a line, to replace a line or to write a line insteade of existing text? In my opinion I did not understand you correctly. Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top