writing to a specific point in an array file

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,
I'm trying to store all of my data into one file (there're about 140 things
to keep track of). I have no problem reading a specific string from the
array file, but I wasn't sure how to replace just one item. I know I can get
the entire array, then save the whole thing (with a for loop and if
statements so that the changed data will be saved), but it seems like a lot
of unnecessary reading and writing. Is there a way to directly save
myArray[whichever number to save] to the correct line in a file?

What I have so far is:
(this one retrieves the entire array that I read from)
public string[] FileToArray(string filename)
{
ArrayList a = new ArrayList();
StreamReader sr = new StreamReader(@"C:\array.txt");
string str;
for (int i = 0; i<139; i++)
{
str = sr.ReadLine();
a.Add(str);
}
sr.Close();
string[] data = new string[a.Count];
a.CopyTo(data);

return data;
}
and then call it with
string[] data = FileToArray(@"C:\array.txt");

And to save the changed info (the 4th index) I use:
string[] data = FileToArray(@"C:\array.txt");
string puppy = "lando";
StreamWriter sw = File.CreateText(@"C:\array.txt");
for (int i = 0; i < 139; i++)
{
if (i != 4){sw.WriteLine(data[i]);}
else {sw.WriteLine(puppy);}
}
sw.Close();

Thanks for any help!
Melanie



.



Relevant Pages