Hi,
I created a data file, which seems to work perfectly, but as far as reading it into an array is where I get stuck. It should be 20 random #s 1 to 99 read into this array in a single line. In my data file, they are all on separate lines, but I need it to read to the console on a single line so that I'll be able to quicksort and bubblesort them later. Please any help would be appreciated.
static void Main(string[] args)
{
FileStream MyFileStream = null;
StreamWriter MyWriter = null;
StreamReader MyReader = null;
int[] myarray = new int[20];
int flag = 0;
// string mystring;
try
{
// create a writer and open the file
MyFileStream = new FileStream("f:\\L08data.txt", FileMode.Create);
MyWriter = new StreamWriter(MyFileStream);
MyReader = new StreamReader(MyFileStream);
// display message to user
Console.WriteLine("Data written to the file.\n");
}
catch
{
Console.WriteLine("Does not exist");
flag = 1; //set flag to indicate file failed to open
}
// mystring = MyWriter.WriteLine();
if (flag == 0) //File opened as expected
{
// output data to the file
Random RandomNumber = new Random();
for (int i = 0; i < 20; i++)
{
int x = RandomNumber.Next(99) + 1;
MyWriter.WriteLine(x);
}
while (!MyReader.EndOfStream)
{
string Mystring = MyReader.ReadLine();
// reads data into an array & outputs data to console
int[] myArray = Mystring.;
Console.WriteLine("{0}\t", MyReader.ReadLine());
}
//Console.Read();
// close streams
MyWriter.Close();
MyFileStream.Close();
MyReader.Close();
}
// exit method and class
return;
}
}
}