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;
        }
    }
}

Recommended Answers

All 4 Replies

Change your while loop to

MyWriter.Close();
String myString;
List<int> myList = new List<int>();
while ((myString = MyReader.ReadLine()) != null) {
    myList.Add(Int32.Parse(myString));
}
int[] myArray = myList.ToArray();

There are better ways to do what you are doing, I'm just giving you something that should work based on what you have.

Thanks for your reply Momerath, but the only thing that posts to the Console is "Data written to file". I used:

Console.WriteLine("{0}\t", MyReader.ReadLine());

following the array statement. What am I doing wrong?

You can not asign a method ReadLine as the parameter of the WriteLine method.

You can do as follows:

while (!MyReader.EndOfStream)
{
    string Mystring = MyReader.ReadLine();
    // reads data into an array & outputs data to console
    int[] myArray = Mystring.Split(' '); //split value by some marks(white spalce, comma, dot, ...)
    for(int i = 0; i < myArray.Lenght; i++)
    {
        Console.WriteLine("{0}", myArray[i].Tostring());
    }
}
Console.ReadLine();

The code above will read all lines a code, and it will write into each line (seperated) all the values read, one by one.

Hope it helps,
Mitja

Mitja, thanks a lot. It looks understandable, but I get an error that 'I cannot implicitly convert an string[]to an int[]. I tried convert

int value =  ConvertToInt.32(myString)

but I get the same error.

Thanks,
Kim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.