-
i have this code, how do i get the program to read the next string after it has done the for statement. that is, i want it to go back to the next string after performing the for statement when its reading the next string
ifstream newfile
string readfile
while(newfile>>readfile){
string temporary="";for (int i=0; i< 2;i++){
newfile>>readfile
temporary+=readfile;
//perform task
}}
-
@emeka_o said in reading from a file:
it doesn’t, it skips one.
It doesn't skip anything.
Let’s say it’s reading integers and it’s on “1” and then in the for statement, it reads 2 3 and 4. I want it to read 2 when it comes back to the the while loop again
That's a totally different thing, or at least you have to make it clear that is what you are asking for.
You now want it to go back from all the strings it read in the
for
loop. There isn't a natural way to do that. Either you will have to remember the "file position" when you first read whatever you want to go back to and do some kind of "seek" back to there (don't know whether C++ streams/>>
operator allow that). Or (maybe simpler) you will have to "save up" your input in an array/list/queue and do your work on that, instead of trying to re-read in the file.Incidentally, this question doesn't seem to have anything to do with Qt.
-
Well, it is the generic C++ forum...but
looks like what the OP is really asking is a token parsing problem that is common in compiler design and has a wealth of academic literature about it. when parsing input streams it is often common to do a peek() of the next token without comsuming() it. Couple that strategy with a recursive descent mechasnim of a token buffer and you will have what you desire.
It is not as simple as reseting the file read pointer back to an earlier token. you have to read in the data into a random access buffer, tokenize the stream, and parse it using recursive decisions about how much of the input to consume.
-
@Kent-Dorfman
Hmm. I've never found my recursive descent algorithms for parsing ever require reading more than one token ahead, or if you prefer backing up by more than one token, so at worst I have a single token buffer. Whereas this user is apparently wanting to back up multiple tokens.