954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading from custom input file?!

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

SelArom
Junior Poster in Training
54 posts since Oct 2004
Reputation Points: 20
Solved Threads: 1
 

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{}
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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

SelArom
Junior Poster in Training
54 posts since Oct 2004
Reputation Points: 20
Solved Threads: 1
 

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

SelArom
Junior Poster in Training
54 posts since Oct 2004
Reputation Points: 20
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You