473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

append to the beginning of a text file

I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.
Nov 20 '05 #1
8 12273
* "Eddie Suey" <es***@inBizServices.com> scripsit:
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.


You will have to move the whole contents of the file...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
trick:

(1) copy the existing data of notepad file to a textbox1
(2) delete the file.
(3) create the file with same name and copy the new text (which you want to
put in the beginning)
(4) now append the file and copy the contents of textbox1

"Eddie Suey" <es***@inBizServices.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.

Nov 20 '05 #3
Eddie,
As the others have suggested you cannot do this.

Largely because of the size of the file. I would consider writing the new
line to a new file, then using Asynchronous File I/O to append the existing
file on the end of this new file, delete the old file, then rename the new
file to the original name...

Similar to:
http://msdn.microsoft.com/library/de...nousfileio.asp

Hope this helps
Jay

"Eddie Suey" <es***@inBizServices.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.

Nov 20 '05 #4
Cor
Hi Eddie,
That is one of first reasons why people started to make databases.
You can now even do more with it,
Maybe a good idea to look if a simple database is not a better approach for
you.
Cor
Nov 20 '05 #5
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
Largely because of the size of the file. I would consider writing the new
line to a new file, then using Asynchronous File I/O to append the existing
file on the end of this new file, delete the old file, then rename the new
file to the original name...


This will change the creation timestamp of the file.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Herfried,
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus
preserving the creation timestamp...

Hmm... Touch.NET (simple sample Console Application demonstrating the above)

Public Module MainModule

' args(0) = file to change
' args(1) = properly formatted creation date time

Public Sub Main(ByVal args() As String)
Dim fi As New IO.FileInfo(args(0))
fi.CreationTime = DateTime.Parse(args(1))
End Sub

End Module

Which will set the creation time of the file given to the date given.

Something like:
Touch myfile.txt 10/1/2003

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
Largely because of the size of the file. I would consider writing the new line to a new file, then using Asynchronous File I/O to append the existing file on the end of this new file, delete the old file, then rename the new file to the original name...


This will change the creation timestamp of the file.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #7
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus


This will work. It was only a little note because most people forget
that the creation date may sometimes be useful...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
Herfried,
Hey! its an important caveat to note!

Not only that it gave me an opportunity to play with System.IO.FileInfo,
something I have not played with a lot.

Thanks
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
If the creation timestamp is important, then this would be a downside of the method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read write, you could 'copy' the attributes from the original file's FileInfo to the new file's FileInfo and that would update the actual file, thus


This will work. It was only a little note because most people forget
that the creation date may sometimes be useful...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Roy Gourgi | last post by:
Hi, How could I append a text file to another text file. Let's say that I have a File1 and I want to append File2 at the end of File1, how would I do that? TIA Roy
3
by: Macca | last post by:
Hi, I am writing some information to a text file using the TextWriter Class. However I am having problems appending data to a file that already exists and already has text in it. I just seem...
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
1
by: Curious | last post by:
I want to write to an output file, "C:\\temp\\debug.txt": If the file exists, append the new content to the end of the file (instead of overwriting the current content). If the file doesn't exist,...
1
by: wish | last post by:
Hello, I have problem is when i append new data in text file always append at the last of the text file. Am i can append the new data in beginning of the text file? Mean when i have update data...
4
by: baburk | last post by:
Hi I want to append one text from textbox1 only at beginning and textbox2 at the end of the file. How do i Do?
1
by: Semajthewise | last post by:
Hi all!! What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.