Reputation: 93
This is the code now:
using (StreamWriter sw = File.CreateText(file))
{
sw.WriteLine("Image Width: " + imageWidth.ToString());
sw.WriteLine("Image Height: " + imageHeight.ToString());
sw.WriteLine("Original Factor: " + originalFactor.ToString());
sw.WriteLine("Current Factor: " + currentFactor.ToString() + Environment.NewLine);
for (int i = 0; i < points.Count; i++)
{
sw.WriteLine("Coordinate " + i + ": " + points[i].ToString());
}
}
}
using (StreamWriter sw = File.AppendText(file))
{
}
If i will use the AppendText it will add new lines. What i want is that if i changed the imageWidth for example of the originalFactor so it will update the value of it in the same line it was written on the first place.
And if the List of points for example had 12 indexs(points) and now i added more points and the List have 20 points now so append the points to those exist do not overwrite them but add the new ones.
All the first 4 lines i want to change/update the values of it and only the List of points to append.
In form1 i have a button click event that is calling a Save method in the new class there im writing the values to the text file.
EDIT**
For the points List matter i did use AppendText:
using (StreamWriter sw = File.AppendText(file))
{
for (int i = 0; i < points.Count; i++)
{
sw.WriteLine("Coordinate " + i + ": " + points[i].ToString());
}
}
But i want to check each time if the points.Count is updated i mean when i click the button to Save again i dont want it to Append the same List of points each time only if the Count of the List was changed bigger then what it was last time.
How can i do it ?
Upvotes: 1
Views: 326
Reputation: 43036
The simplest approach is probably to overwrite the entire file with a new file.
If you definitely need not to do that, then you can use the Position
property of the stream, or the Seek
method, to get your new data into the same part of the file where the old data was. Then you have another problem to solve, which is that if the new data occupies a different number of bytes than the old data, you will have to shift all the bytes that occur after the changed data to account for that. For example, suppose your original file was this:
...
Current Factor: 9
Coordinate 1: (1, 1)
...
Now you want to change it to this:
...
Current Factor: 10
Coordinate 1: (1, 1)
...
If the word "Coordinate" used to start at the 100th byte in the file, you'll have to move it -- and everything after it -- to start at the 101st or 102nd byte of the file (depending on the text encoding you're using) before you write the new "Current factor" value. Also don't forget that you'll have to move the CRLF that appears before the word "Coordinate".
Similarly, if you were changing the value from "10" to "9", you'd have to shift everything after that number one or two bytes earlier in the file, and you would also have to reduce the file's length by one or two.
Rather than solving these problems, most applications I've seen just write a new file, replacing the lines that need to be replaced, then delete the old file and move the new file to the original file name.
Upvotes: 3