473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inserting a new line of text at begin of file

Hi,

i would like to know what approach i should use to insert (not append)
a line of text to the begin of an existing text file.

My current approach works, but i am afraid of loosing data if an
exceptions is thrown somewhere in between the process.

My current approach:
1. read current contents of the file
2. create a filestream object (with FileMode.Create ) wrapped in a
streamwriter object
3. write the new line of text
4. write the content that was read in step 1

im looking for a more transactional approach.

Thanks in advance.

Cheers,
Jaap

Aug 16 '06 #1
3 2974
Jaap,

You won't be able to get it. Until Vista is released, you won't be able
to incorporate transactional behavior into file operations.

However, I do think you should order your approach somewhat differently.
I would open the new file stream and write the contents, then open the old
stream and copy the contents over. There is no reason to keep the current
files contents in memory while you write the new content (unless you need it
for some other reason).

When Vista comes out, you should be able to use a FileStream in a
TransactionScop e and you should be able to perform your file operations as
part of a transaction. If it is not built into the FileStream, you will
always be able to drop down to the API level to perform transactional work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"japi" <jv******@gmail .comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,

i would like to know what approach i should use to insert (not append)
a line of text to the begin of an existing text file.

My current approach works, but i am afraid of loosing data if an
exceptions is thrown somewhere in between the process.

My current approach:
1. read current contents of the file
2. create a filestream object (with FileMode.Create ) wrapped in a
streamwriter object
3. write the new line of text
4. write the content that was read in step 1

im looking for a more transactional approach.

Thanks in advance.

Cheers,
Jaap

Aug 16 '06 #2
Hi Nicholas,

thank you for looking into this problem.

The reason why i keep the content in memory, is because the source and
destination filestreams map to the same physical file. I read the
contents into a string before i close the reading stream. Then i open a
new stream to the same file for writing purposes.

Again, thank you.

Cheers,
Jaap

Nicholas Paldino [.NET/C# MVP] wrote:
Jaap,

You won't be able to get it. Until Vista is released, you won't be able
to incorporate transactional behavior into file operations.

However, I do think you should order your approach somewhat differently.
I would open the new file stream and write the contents, then open the old
stream and copy the contents over. There is no reason to keep the current
files contents in memory while you write the new content (unless you need it
for some other reason).

When Vista comes out, you should be able to use a FileStream in a
TransactionScop e and you should be able to perform your file operations as
part of a transaction. If it is not built into the FileStream, you will
always be able to drop down to the API level to perform transactional work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"japi" <jv******@gmail .comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,

i would like to know what approach i should use to insert (not append)
a line of text to the begin of an existing text file.

My current approach works, but i am afraid of loosing data if an
exceptions is thrown somewhere in between the process.

My current approach:
1. read current contents of the file
2. create a filestream object (with FileMode.Create ) wrapped in a
streamwriter object
3. write the new line of text
4. write the content that was read in step 1

im looking for a more transactional approach.

Thanks in advance.

Cheers,
Jaap
Aug 16 '06 #3
"japi" <jv******@gmail .comwrote in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
The reason why i keep the content in memory, is because the source and
destination filestreams map to the same physical file. I read the
contents into a string before i close the reading stream. Then i open a
new stream to the same file for writing purposes.
using Nicholas's suggestion, you can write to a new "temp" file, then if all
is well, do the delete and rename. I would think there would be more
opportunities for correcting unexpected errors along the way in this
fashion.

-- Alan
Aug 16 '06 #4

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

Similar topics

65
12569
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
1
1363
by: Yama | last post by:
Hello, Can someone tell me how to insert text into a table containing a TEXT field? How to update? I would like to create a stored procedure that take a text parameter and inserts it into a new column in my table. Then do the same stored procedure but this time to update the TEXT field. HELP!
1
3711
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO CREATE TABLE dbo.aTable ( INT NOT NULL
9
5205
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
4
3651
by: Zenon | last post by:
I have been tasked with writing code which overrides the TraceListener class for the purpose of limiting the size of the log file. Basically, when the log file grows to a certain size, I need to begin overwriting it from the beginning. I am using a ReadWrite Stream to do this. My problem is the fact that the lines which are to overwrite the old ones are not necessarily going to be the same size. For instance, if the first line in the...
3
4269
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be created and I could be able to insert data. But with this code below I've failed could someone help me and tell me where I'm going wrong: private void Page_Load(object sender, System.EventArgs e) { if (! IsPostBack)
3
1974
by: td0g03 | last post by:
Hello, I am trying to write a code to copy a file which I can do as you can see below. void copy () { // Local Definitions int c; int closeStatus; FILE* spProverbs ; FILE* spProverbsCopy;
12
4908
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
5
5043
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a file: //This is my source file //this is where i want to insert a line class Class1
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.