473,394 Members | 1,870 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,394 software developers and data experts.

prepending text to a file

Hi

I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file.
Is there a way, not necessarily with streamwriter, that I can open a file
and prepend text, so that the newly added data appears at the head of the
file?

Thanks

Tony
Nov 15 '05 #1
3 10209
Tony,

I don't think that there is an easy way to do this. What I would do is
create a new temp file and then write your information. Once you have that,
I would open the other file and then write the contents of the file to the
temp file after the information you have written.

Finally, you would delete the old file and then copy the temp file over
to the old file location when done.

If you want this all to happen in the same file, then you might want to
create a memory-mapped file and then perform a memory copy (CopyMemory) to
shift the contents of the file over and then write your data at the
beginning of the file.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tony" <La*********@homeandresting.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi

I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file.
Is there a way, not necessarily with streamwriter, that I can open a file
and prepend text, so that the newly added data appears at the head of the
file?

Thanks

Tony

Nov 15 '05 #2
Tony <La*********@homeandresting.com> wrote:
I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file.
Is there a way, not necessarily with streamwriter, that I can open a file
and prepend text, so that the newly added data appears at the head of the
file?


No. For one thing, I don't know of any mainstream filesystem which
supports this, or at least makes its support widely known.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Correct, prepending is a difficult operation to perform... if its text then
stringbuilder can be loaded with the current content, then written back out
to the same file, but obviously if the file size becomes very large it would
get slower and slower and slower.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tony <La*********@homeandresting.com> wrote:
I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file. Is there a way, not necessarily with streamwriter, that I can open a file and prepend text, so that the newly added data appears at the head of the file?


No. For one thing, I don't know of any mainstream filesystem which
supports this, or at least makes its support widely known.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

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

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
3
by: Stefan Behnel | last post by:
Hi! I'm writing a parser using pyparsing and I would like to augment the ParserException tracebacks with information about the actual error line *in the parsed text*. Pyparsing provides me with...
4
by: AHP | last post by:
Hi, I'm using Visual Studio 2005. I am developing a web application that uses the FileUpload control to upload text files to a directory on a webserver. This works fine. However, for me to be...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
3
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: ...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.