Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. reading from a file

reading from a file

Scheduled Pinned Locked Moved Unsolved C++ Gurus
6 Posts 3 Posters 798 Views
  • 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 Offline
    E Offline
    emeka_o
    wrote on last edited by
    #1

    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
    }

    }

    JonBJ 1 Reply Last reply
    0
    • E emeka_o

      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
      }

      }

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @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
      0
      • JonBJ JonB

        @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 Offline
        E Offline
        emeka_o
        wrote on last edited by
        #3

        @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

        JonBJ 1 Reply Last reply
        0
        • E emeka_o

          @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

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @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
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            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.

            JonBJ 1 Reply Last reply
            0
            • Kent-DorfmanK Kent-Dorfman

              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.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

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

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved