hello! I tried posting this in another community forum and didn't get much success...

is there no way to duplicate the >> operator functionality in C#? I have a file that is set up like this:

450 350 0 0 10 390 10 20 40 20 0 1 1 0 20 409 ...

where the first two numbers are the dimensions of an array and the next 450 numbers are the first row of that array, the next 450 numbers are the next row, and so on to fill 350 rows, and they are all on one line.

so I need a way to do something like:

input >> firstNum;
input >> secondNum;
for (i = 0; i < firstNum; i++)
     for (j = 0; j < secondNum; j++)
     {
           input >> tempInt;
           theArray[i,j] = tempInt;
     }

that might not be syntactically correct because I'm not too strong in c++ either :( but I know that it's something like that, and that's what I want to do in C#, but all the stream inputs use readline() which will grab the whole humongous line and that's not what I want.

i looked into the binary reader but I can't seem to get what I want from that either... am I screwed?

-SelArom

use the readline() to get the one long string (called input in this code), then do the following

int[,] integerarray;
			char[] separator = {' '};
			int x, y;
			string[] inputArray = input.Split(separator);
			try
			{
				x = Convert.ToInt32(inputArray[0]);
				y = Convert.ToInt32(inputArray[1]);
				integerarray = new int[x,y];
				int x2 = 0;
				int y2 = 0;
				for(int i = 2; i < inputArray.Length; i++)
				{
					integerarray[x2, y2++] = Convert.ToInt32(inputArray[i]);
					if(y2 == y)
					{
						y2=0;
						x2++;
					}
				}
				string result = x + y + "";
				
			}
			catch{}

I had already considered reading the whole line but then I would have two copies of over 200,000 numbers in memory. I would like a way to process the integers directly into the two dimensional array from the file... I did manage to accomplish this using the code below:

private static int readInt(ref BinaryReader BReader)
{
	StringBuilder buffer = new StringBuilder(4);
	while (BReader.PeekChar() != ' ')
		buffer.Append(BReader.ReadChar());
	BReader.ReadChar();
	return(Convert.ToInt32(buffer.ToString()));
}

but that is really inelegant and ineffecient. I think the best thing would be to process the file then put each integer on a seperate line... but I would really like a better way to input c++ style ...

thanks for your reply though!
-SelArom

is there no better way to do this? it takes about 5 seconds to load the data into memory!! is there not a faster way to process a line of numbers and input them one by one?

-SelArom

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.