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

How to update a text file

Hi,
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.

Thanks
David
May 17 '07 #1
5 18934
Hi,
"DavidE" <Da****@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
Hi,
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.
You create a temp file, copy the unchanged lines without modification,
modify & write the one you need to modify and then copy back the temp file
to the real one.
May 17 '07 #2
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.
Create a new temporary file and write everything to that instead. On
successful completion, replace the original. It's easier and safer (i.e., if
something goes wrong, delete the temporary and the original remains intact).
May 17 '07 #3
"DavidE" <Da****@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.
If the lines you are updating do not change in length, you can update
them using a FileStream, which has Read() and Write() functions that can
read and write any number of bytes at any offset in the file. Basically, you
would be treating the file as a binary file instead of a text file.
However, if you are replacing arbitrary length lines, the only easy
solution is to create another file and write it anew. You cannot simply
replace some lines in the middle of the existing file because the different
length of the lines would cause all the rest of the file to "move" up or
down, so you would have to rewrite it on disk anyway.

May 17 '07 #4
David,

You can't just update a few lines in the middle of a file. Rather, you
will have to write your changes at the appropriate point in the file, and
then write the information that you want to write after the changes back to
the file (for the rest of the file).

Say you wanted to insert the string "Hello" (ASCII characters) into the
file. You would have to go to the point you want to insert it, and read 10
bytes from the file, storing them in a buffer (twice the length of the
string you want to write). I'd suggest storing them in two arrays of five
bytes each. Then you write "Hello". Once you do that, you can write the
first five bytes of the buffer after hello. Then, read the next five bytes
in the file and store them in the buffer (this is why I recommend two
buffers, so you can just swap references as opposed to having to copy all
the bytes around in a single buffer). You then keep repeating this for the
rest of the file.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DavidE" <Da****@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
Hi,
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.

Thanks
David

May 17 '07 #5
Hi,

Thanks You all for your help. I realy appreciate it. Now I have a good
direction to solve this problem.

Have a good week .

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

You can't just update a few lines in the middle of a file. Rather, you
will have to write your changes at the appropriate point in the file, and
then write the information that you want to write after the changes back to
the file (for the rest of the file).

Say you wanted to insert the string "Hello" (ASCII characters) into the
file. You would have to go to the point you want to insert it, and read 10
bytes from the file, storing them in a buffer (twice the length of the
string you want to write). I'd suggest storing them in two arrays of five
bytes each. Then you write "Hello". Once you do that, you can write the
first five bytes of the buffer after hello. Then, read the next five bytes
in the file and store them in the buffer (this is why I recommend two
buffers, so you can just swap references as opposed to having to copy all
the bytes around in a single buffer). You then keep repeating this for the
rest of the file.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DavidE" <Da****@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
Hi,
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.

Thanks
David


May 20 '07 #6

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

Similar topics

1
by: revolnip | last post by:
As attached is the code : <% Option Explicit dim lngTimer lngTimer = Timer %> <!--#include file="Connect.asp" --> <!--#include file="Settings.asp" --> <!--#include file="Common.asp" -->
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
2
by: TJ | last post by:
Hi All, I am having some trouble. I have created a database via the new database option inside VWD2005. Then and table or two. I have been able to perform INSERT and SELECT operations on...
1
by: Selvakumar | last post by:
hai friends, I am new to .net programming. I did the inserting data into MS-access database but i couldn't able to perform the update command. I used only textbox and command button for inserting...
0
by: yemen2007 | last post by:
hi i have some problem in my project i want help . the problem it is update or edit to the file in xml.my project is write and update/edit to xml file. i done the first one is write bout i have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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...

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.