This is pretty much what I am doing write now. This way I end up writing a 10 GB file line by line when all I had to do was replace some text in the existing file.
Main Topics
Browse All TopicsI am using System.IO to read large text files (10GB). Each line in these files is a record, and the main pupose of this application is to validate the data. This I do by reading the file line by line and looping thru the lines and all's fine n dandy. Until now.
Here's where the problem is - for certain records (lines) in a very few files of these, I need to edit a part of the line. that is.. replace text in the middle for a few files.
The only way I could accomplish this so far was by using a separate Intermediate file and writing to it. This obviously is not the most optimized way to do it.
1. What would be ideal would be to edit text while reading the file.
2. If that cannot be done, I could Close the StreamReader and edit text at that particular position (using StreamWriter.basestream.se
Anyway, neither 1 nor 2 is working for me. Can anyone help please.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes... You will be forced to use this aproach. You cant "just" cut a tiny bit out of a textfile and expect the end of the file to "move up"
What will the replacement be? Will it ALLWAYS reduce the linesize keep it the same? If thats the case you could consider a "binary" aproach. So you could update the "current row" and pad the end with some withspace if you removed something. But this wont work if there is even the slightest chance of the file increasing in size. If you have a chance of increasing the linesize then you would have to implement a buffer also and make sure you dont overwrite data you havent written yet. Also this method can messup the file. If you fail halfway for some reason the file could take some damage since you are operating on the "live" file.
Is there any chance to reduce the filesize by changing it from "monthly" to "daily" (or whatever you are logging) mode?
Another option is to modify the tool that is reading the file. If this tool could do the modification while it is importing the file you would not have to do it before you import it. Also this would allow you to keep the original data (I dont like loosing my "original data"). Where / How is the file imported later? Is it a DTS package?
I was just thinking loud here....
All valid points...
The "Original" file is archived before I process - while that does not imply that I am not worried about corrupting data in the file - I got to change it anyway. These files are FTPed from all over the country - so there's no way that I could do anything while importing (and I do not import anyway).
I am inclining towards using binarystreams - the line size is const. - my only concern is if it would have a big impact on memory usage. (so far it does not seem to be a memory hog... I believe the reason being that a binarystream / filestream object is already persisted in the form of the file itself and so does not adds to memory usage.. am I making any sense??)
oh.. and btw I would not be forced to use the approach that gregasm suggested.
This is what I do -
1. I read the file line by line..
2. Store the positions where I have to make changes and corresponding text in a 2D array.
3. close the streamreader
4. open a filestream on the same file.
5. Set the filestrampositions reading from the array
6. use the writebyte method to replace text again from the same array
I think it's better than writing than replicating the entire file.
Business Accounts
Answer for Membership
by: gregasmPosted on 2004-11-15 at 19:22:15ID: 12590126
How about this:
rrentRecor d)
Open two streams. One StreamReader to read, and another FileStream to write.
While StreamReader.ReadLine() = strCurrentRecord, validate the record, and perform any modifications to the record.
Then FileStream.WriteLine(strCu
This would seem to work, while not consuming too many resources in the process.