Hi
I am trying to find a way to:
1. Edit Data
2. Delete Data
From a file using c#
I read up online and it seems everyone's solution is to rewrite or either recreate the file without the wanted information but I doubt that is the most efficient way...and for what I'm trying to do that just wont work.
Example.
I have a text file with the following Lines
1. TEST2
2. TEST2
3. TEST3
If I want to replace Line 2 with TEST8 it's easy by using
byte[] bytes = Encoding.UTF8.GetBytes(Environment.NewLine + "TEST8" + Environment.NewLine);
fs.Seek(XXX,SeekOrigin.Current);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
But if I want to
replace it with "Testing123" it will start overriding the bytes of line 3.
Is there any way it can be done ? can I move the remaining bytes to the right to avoid the override ?
Or is there a easier way to do this ?
Also I would is I would like to completely Delete a line from the file.
Thx
Rousseau
Loading
Sam HobbsPosted Jun 24, 2011, 1:10 PM
Rousseau SennettPosted Jun 24, 2011, 3:55 AM
http://www.codeproject.com/KB/cs/InsertTextInCSharp.aspx
which gave me the info I needed to move my bytes around in the file...
because padding would have made my file larger... although I doubt that would really have mattered... anyway all I did was write my own Method to this class for deleting bytes by just moving all the bytes to the right of my position to the left for the amount of bytes I want to delete and setting the file length to "Length" – "Number of bytes Deleted".
Also this class includes a Method Find() which displayed some weird behaviour I didn't really go through the code to find out why the find method behaved like this was just easier to write my own
But when I used ex.
12345<>
And Ran Find("<>") it would return position 5;
And if I change the text to.
<><>
And ran Find("<>") it would return 4;
Sam HobbsPosted Jun 21, 2011, 3:47 AM
This is the type of thing that a database is good at; they are designed to allow efficient update of data.
One way to do it is to design your file such that it has padding among the records. The application could be designed so that it ignores the padding when reading and it could go past the padding when writing data so that the padding is retained. If however new data exceeds the space occupied by data being replaced, then the padding could be over-written and that would eliminate the need to re-write all data. The filw would need periodic maintenance to ensure that there is a sufficient amount of padding.
Another way is to have a list of offsets into the file and the list could be ordered in the order that the records are supposed to be ordered in. Then the new data could be put anywhere and only the list of offsets needs to be updated. For example in your test data the first record would occupy either 5 or 7 bytes depending on whether the data included end-of-line bytes; let's say 5 bytes (no end-of-line bytes). So the second record would be at offset 5. A new record could be added at the end (offset 15) and then the list of offsets would be:
0
15
10
VulpesPosted Jun 20, 2011, 9:23 AM
Similarly, you can delete a line (say 'n' bytes long) by moving all the bytes in the file immediately following it 'n' bytes to the left and then calling FileStream.SetLength() to truncate the file size by 'n' bytes.
See http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx