Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved reading from a file

    C++ Gurus
    3
    6
    389
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      emeka_o last edited by

      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
      }

      }

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @emeka_o last edited by

        @emeka_o
        Whatever this is doing, after the for loop it returns to the while(newfile>>readfile) which seems to read the next string.

        E 1 Reply Last reply Reply Quote 0
        • E
          emeka_o @JonB last edited by

          @JonB it doesn’t, it skips one.
          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

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @emeka_o last edited by JonB

            @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.

            1 Reply Last reply Reply Quote 0
            • Kent-Dorfman
              Kent-Dorfman last edited by

              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.

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Kent-Dorfman last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post