Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from files

Status
Not open for further replies.

andybeeeeee

Programmer
Dec 9, 2002
17
GB
Trying to read some data from a file. I'm reading in 4 lines and storing them in a 2D array and then starting again. Here's the code i'm using:

Code:
Console.WriteLine("Please enter the absolute path of the file: ");
				string path = Console.ReadLine();
				String[] TempArray = new String[100, 4];
				FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
				StreamReader sk = new StreamReader(file);
				for (int i=0; i<99; i ++) 
				{
					string s = sk.ReadLine();
					s = TempArray [i, 0];
					string t = sk.ReadLine();
					s = TempArray[i, 1];
					string u = sk.ReadLine();
					s = TempArray[i, 2];
					string v = sk.ReadLine();
					s = TempArray[i, 3];
				}
				sk.Close();
				file.Close();

when i try to it i get quite a few errors which i dont understand. can anyone help me with the following:

Cannot implicitly convert type 'string[*,*]' to 'string[]'

Wrong number of indices inside [], expected '1'

The second error applies to multiple lines inside the for loop, each of the lines when i reference the array

Any help?

 
ok.. replace your code with this
Code:
Console.WriteLine("Please enter the absolute path of the file: ");
                string path = Console.ReadLine();
                String[] TempArray = new String[100, 4];
                FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
                StreamReader sk = new StreamReader(file);
                int i = 0;
                while(sk.Peek())
                {
                    TempArray [i][0] = sk.ReadLine();
                    if (sk.Peek()) TempArray [i][1] = sk.ReadLine();
                    if (sk.Peek()) TempArray [i][2] = sk.ReadLine();
                    i++;
                }
                sk.Close();
                file.Close();

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
this didnt help, just got the same amount of errors as before, but different errors.

why cant i do it the way i had it before? that's the way i've always done it in java...

I still get the array error about converting [*,*] to [] but i also get errors about converting strings to chars and bools????
 
ah nevermind, fixed it now. all because of one little comma!!!
 
i didn't check it...


but this compiles to me
Code:
			Console.WriteLine("Please enter the absolute path of the file: ");
			string path = Console.ReadLine();
			string[,] TempArray = new string[10, 10]; 
			FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
			StreamReader sk = new StreamReader(file);
			int i = 0;
			while(sk.Peek() != -1)
			{
				TempArray [i,0] = sk.ReadLine();
				if (sk.Peek() != -1) TempArray [i,1] = sk.ReadLine();
				if (sk.Peek() != -1) TempArray [i,2] = sk.ReadLine();
				i++;
			}
			sk.Close();
			file.Close();

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top