A question about streaming --

Started by
5 comments, last by OpenGL_Guru 19 years, 5 months ago
i am reading an input file and in certain cases in the file i need to be to able "look ahead" in the file to the next character see what is next before actually reading it. the character would be a number or it can be a # sign. if its a # sign then i can go on and check the next character. if not then i can go ahead and convert the character to integer and use it from there. i remember glancing over this long ago but never thought i would actually need it. i looked on msdn all i could find was seekg but i dont think that was it. can anyone help me on this?
heh
Advertisement
int ungetc(int character, FILE *stream);

edit: I just saw the seekg() in the OP. Is there anything equivalent for C++ streams?
You can use the method std::istream::peek the other alternatives are to "get" a character and then put it back with std::istream::putback or consume a character and call std::istream::unget to move the get pointer back one, you don't need to do parsing your self as the iostreams already do so use a combination of unformatted & formatted operations.
thanks guys ill try that out, defintely what i need by pushing the pointer back in the input file. but i thought there was a function to somehow return a character ahead in the file before you even read it? maybe i was thinking of seekg i dunno... hmmm anyway thanks again..
heh
Quote:Original post by OpenGL_Guru
thanks guys ill try that out, defintely what i need by pushing the pointer back in the input file. but i thought there was a function to somehow return a character ahead in the file before you even read it? maybe i was thinking of seekg i dunno... hmmm anyway thanks again..
You should try to avoid unnecessary seeking since the input file might not be seekable. Just read one character ahead an put it back onto the stream again with ungetc(), that's what it's for.
ok will do thanks again :)
heh
i am not using pointers to my files , rather i am declaring my streaming variables of type ifstream....in my class i have already declared infile4 to be of type ifstream. this is what the first few lines of the input file are..so as you can see i have used the peek function to look ahead to see in advance what is in the file before ever actually reading it in from the file. it doesnt seem to want to read the #sign the next go round. for some reason the last line before the next #sign is outputted twice but the streaming goes no further than that. input/code/output below. can u tell me what is going on here? thanks in advance! oh yeah coast_lat and coast_lon are doubles.

# -b
-81.499570 31.385840
-81.499570 31.385693
-81.499790 31.385766
-81.500084 31.385986
-81.500010 31.386133
-81.499570 31.385840
# -b
-81.396016 31.387233
-81.395869 31.387453

my code is as follows:

/*in function*/
char line_break;
char ch1; char ch2;
char c;

while(infile4) //while not EOF
{
c = infile4.peek(); //look ahead
if(c == '#')
{
infile4 >> line_break >> ch1 >> ch2; //read# -b
outfile << endl;
cout << line_break << ch1 << ch2 << endl;
}
else
{
infile4 >> coast_lon >> coast_lat;
outfile << coast_lon << " " << coast_lat << endl;
}
}

console output:

#-b

outfile output:

-81.4996 31.3858
-81.4996 31.3857
-81.4998 31.3858
-81.5001 31.386
-81.5 31.3861
-81.4996 31.3858
-81.4996 31.3858
heh

This topic is closed to new replies.

Advertisement