Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Manipulate the file pointer

Manipulate the file pointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 2.9k 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.
  • quentinthorntonQ Offline
    quentinthorntonQ Offline
    quentinthornton
    wrote on last edited by
    #1

    Hey guys, I have a couple questions about manipulating file pointers that I can't find anywhere online. First, how would I go back a line in a file? I tried counting the line number and then reseting the QTextStream object, and lastly I made a loop

    while(lineNum > 0){//starts at beginning of file and works it's way to the line number that I want it to go to
    (QTextStream object) s.readLine();
    }
    

    and when it was done it should have brought me to the line I wanted. I know that's not right (since it didn't work), and it doesn't seem very efficient. I was just wondering if there was a way I could go back one line and forward a line with ease. I tried going back by using it like a pointer like this:

    *(QTextStream object) s -= 1;// this right here is exactly what I want to do. Is there another way to do this? 
    

    but as you can probably guess, it didn't work. Last question, how does the readLine() method know when it is at the end of a line? Is there a \n character at the end? Thanks for your time.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by alex_malyu
      #2

      Once you are able to answer questions below you will be at least closer to solution.

      1. what is s?

      2. did the line below compile? Why it did not and what you expected
        (QTextStream object)
        should do and why did you want it to be done?

      (QTextStream object) s.readLine();

      1. When this loop exits?
        while(lineNum > 0){

      (QTextStream object) s.readLine();
      }

      1. And once you answer #2 explain *(QTextStream object)

      In any case I would recommend C++ book first, Without it you will not move far,

      quentinthorntonQ 1 Reply Last reply
      0
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @quentinthornton,

        If your QTextStream is seekable, then you might be able to do something with QTextStream::pos and QTextStream::seek, like:

        qint64 previousLinePos = s.pos();
        QString line = s.readLine();
        if (/* ... */) {
            // go back one line
            s.seek(previousLinePos);
        }
        
        1 Reply Last reply
        0
        • quentinthorntonQ Offline
          quentinthorntonQ Offline
          quentinthornton
          wrote on last edited by
          #4

          Thanks a lot for your reply. I've tried that but I can never get it to work. Maybe someone could help me fix it? Thanks.

          This is a part of my code. Before this, it looks to see if two sentences are similar (one is from a file, the other is user inputed. If at least 60% of the words match, then it will return the line after the line that it matched with. If it is over 80 and less than 100 %, it should store the user inputed sentence right next to the sentence in the file. After that is done, it should still return the next sentence after the one that matched with the inputed sentence.

           if(percentage >= 60){ //if percentage of words matching is greater than 60
                         if(percentage >= 80 && percentage < 100){
                             textStream.seek(lastLine + userInputedSentence.size());//This is supposed to put it right after the sentence that it matched with. lastLine is the position of the line currently being worked on. For some reason, the readLine() function moves the pointer down one line after it's done (\n or something). That's why I need to do this.
                             textStream<< " " << userInputedSentence;// This should put a space between the last sentence (so it's not scrunched together) and then adds the user inputed string next to it.
                         }
                         line = textStream.readLine()
                         }
          return line;
          

          Any Ideas what I did wrong? Thanks.

          1 Reply Last reply
          0
          • quentinthorntonQ Offline
            quentinthorntonQ Offline
            quentinthornton
            wrote on last edited by
            #5

            Sorry, I forgot to say, it ends up putting the sentence at the end of the file regardless the fact that I have all that.

            1 Reply Last reply
            0
            • A alex_malyu

              Once you are able to answer questions below you will be at least closer to solution.

              1. what is s?

              2. did the line below compile? Why it did not and what you expected
                (QTextStream object)
                should do and why did you want it to be done?

              (QTextStream object) s.readLine();

              1. When this loop exits?
                while(lineNum > 0){

              (QTextStream object) s.readLine();
              }

              1. And once you answer #2 explain *(QTextStream object)

              In any case I would recommend C++ book first, Without it you will not move far,

              quentinthorntonQ Offline
              quentinthorntonQ Offline
              quentinthornton
              wrote on last edited by
              #6

              @alex_malyu
              Sorry, I just barely saw your post. For some reason I missed it. Thanks for taking the time to read through my post and reply. I put the (QStreamObject) because I was thinking some people might not know what 's' alone meant. I guess I had it right up there in my explanation. Sorry about that. :)

              kshegunovK A 2 Replies Last reply
              0
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You cannot insert in a file, you can only append or overwrite. If you want to insert a sentence after another one, then you have to do following:

                • Create new file
                • Copy everything up to the sentence after which you want to insert (including the sentence itself)
                • Write the new sentence
                • Copy everything from the old file after the sentence after which you wanted to insert
                • Delete old file
                • Rename the new file

                As alternative you could move the content after the sentence after which you want to insert inside the same file.

                So, your current approach will not work - it will overwrite existing sentences.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • quentinthorntonQ quentinthornton

                  @alex_malyu
                  Sorry, I just barely saw your post. For some reason I missed it. Thanks for taking the time to read through my post and reply. I put the (QStreamObject) because I was thinking some people might not know what 's' alone meant. I guess I had it right up there in my explanation. Sorry about that. :)

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @quentinthornton
                  Additionally to what @jsulm noted, you can't seek in a text file. There's encoding involved and the lines are of different lengths, so there's no way to tell before reading how much bytes (because seek and tell operate on bytes) you have to move.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • quentinthorntonQ quentinthornton

                    @alex_malyu
                    Sorry, I just barely saw your post. For some reason I missed it. Thanks for taking the time to read through my post and reply. I put the (QStreamObject) because I was thinking some people might not know what 's' alone meant. I guess I had it right up there in my explanation. Sorry about that. :)

                    A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by alex_malyu
                    #9

                    @quentinthornton

                    (QTextStream object) is not QStreamObject and none of them made any sense to me,
                    If you need comment within the code, use standard C++ comments,
                    otherwise you confuse people

                    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