writing to a specific point in an array file
- From: melanieab <melanieab@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 19 Jul 2005 14:35:03 -0700
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
.
- Follow-Ups:
- RE: writing to a specific point in an array file
- From: Mark R. Dawson
- RE: writing to a specific point in an array file
- Prev by Date: Re: Problem Accessing HTMLElementCollection
- Next by Date: Re: Problem Accessing HTMLElementCollection
- Previous by thread: C# Interop with Remote COM Object
- Next by thread: RE: writing to a specific point in an array file
- Index(es):
Relevant Pages
|