Manipulate the file pointer
-
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.
-
Once you are able to answer questions below you will be at least closer to solution.
-
what is s?
-
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();
- When this loop exits?
while(lineNum > 0){
(QTextStream object) s.readLine();
}- And once you answer #2 explain *(QTextStream object)
In any case I would recommend C++ book first, Without it you will not move far,
-
-
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); }
-
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.
-
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.
-
@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. :) -
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.
-
@quentinthornton
Additionally to what @jsulm noted, you can'tseek
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 (becauseseek
andtell
operate on bytes) you have to move. -
(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