file i/o with varying size numbers

Started by
6 comments, last by shyam 20 years ago
I''ve invested about 9 hours so far trying to figure this out so I decided to post here. There is probably a simple solution that I am overlooking, but I can''t find it for the life of me. I have an input file that has 128x128 array entries on it. Each entry is separated by a tab ''\t'', character. Here is the problem.. the file is set up something like this: 001 - 1 33 11 1 2 3 ..etc.. 002 - 33 1 2 623 1 5 ..etc.. The 001, 002, etc on the left are the row number (just for human readability). I want to simply get the array entries (1, 33, 11, 1, 2, 3) and put them into my array (type int). I''ve successfully written the code to read 1 char at a time, cast to ints, and store them in the array.. however... it only works for SINGLE DIGIT numbers. The numbers can be 1-3 digits long. How do I do this? I''ve tried methods getting 1 char at a time, but I ran into the above issue. I tried getting an entire line at once, but I ran into the above issues plus issues with ignoring the first 5 chars (the line number and such). I''ve tried using atoi() functions... no luck. Any hints/solutions would be hugely appreciated Thanks.
"The results are interesting, if true, but they are certainly not going to help me find oil."- response of Chevron Oil VP after a scientist working for him discovered 2^216,091 - 1 was a prime number.
Advertisement
Try using fstream and the << and >> operators. Your file setup is almost ideal for this method.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
What programming language are you using? Assuming C++, you can just the extraction operator with the file stream to get at the numbers. But it''s probably best if you show the code that you were using that didn''t work. Then we can make specific suggestions.
fscanf - MMmmMMmmm, my favorite.
Brianmiserere nostri Domine miserere nostri
Here's the code. I've never used >> except with keyboard input. I read the documentation, and I thought I was doing it right, but I guess not.

When I run the program, it exits immediately and prints nothing to the output file (the printing code isn't shown, but it works).

EDIT: oh, and yes it's C++

	input_file.get(ch);	// strip out the comments	while (!input_file.eof()) {						// while not end of the file		// if not a comment, line spacer, return or a space, parse it		if (ch != ';' && ch != '-' && ch != '\n' && ch != ' ') {			input_file.ignore(5);					// ignore the characters for human readability			while (ch != '\n' && !input_file.eof()) {				input_file >> temp;				CurrentMap.base[y][x] = (unsigned char)temp;				input_file.ignore(1);				x++;								// next column				if (ch == '\n') {					// new row					x = 0;								// set x back to the far left column					y++;								// increment y to the next row				}			}		}		else {										// else			while (ch != '\n') {						// it's a comment (semi-colon),  go to the next line!				input_file.get(ch);			}		}		input_file.get(ch);	}  


[edited by - shyam on April 12, 2004 6:56:48 PM]

[Edit by Oluseyi: Because horizontal page scrolling is just wrong.]

[edited by - Oluseyi on April 12, 2004 7:57:51 PM]
"The results are interesting, if true, but they are certainly not going to help me find oil."- response of Chevron Oil VP after a scientist working for him discovered 2^216,091 - 1 was a prime number.
int ary[50][50];ifstream if(filename);for(int lines=0;lines<50;lines++) {  if.ignore(6); // "### - " 6 chars ignored  for(int cols=0;cols<50;cols++) {    if >> ary[lines][cols];  }  if.ignore(999,'\n'); // eat the newline character}//done


of course, you should make the stupid pseudocode mistake of naming you ifstream after a reserved word

[edited by - C-Junkie on April 12, 2004 7:21:06 PM]
I love you sir

The problem was partially with my input.. But it was mainly with my output. I was attempting to print 1 char at a time to out_file. woops

One last question: How can I test for end of file using cin?

[edited by - shyam on April 12, 2004 7:57:16 PM]
"The results are interesting, if true, but they are certainly not going to help me find oil."- response of Chevron Oil VP after a scientist working for him discovered 2^216,091 - 1 was a prime number.
quote:Original post by shyam
One last question: How can I test for end of file using cin?
cin.eof()

You can do this to all istreams, including ifstreams.

This topic is closed to new replies.

Advertisement