change/add letter in text file c++
-
@RuWex
I think we have all tried to answer as best we can.You cannot update a text file "in place" or "as you go" or "only certain lines". You have to do the whole text file, from beginning to end. You can do that either by using a second file for writing as you read or by doing the work on an in-memory copy of the file and then writing it back.
-
@JonB YES, this is I understood
my question was if I can change ALL the file while the function readfile reading,
Imean every time that found # to change all file?@RuWex said in change/add letter in text file c++:
if I can change ALL the file while the function readfile reading,
Imean every time that found # to change all file?Why not? It's up to you what you write back into the new file.
-
@RuWex said in change/add letter in text file c++:
if I can change ALL the file while the function readfile reading,
Imean every time that found # to change all file?Why not? It's up to you what you write back into the new file.
@Christian-Ehrlicher thank!!
-
I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.
My claims are fully untested, so I might also be mistaken about the details.
If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)
-
I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.
My claims are fully untested, so I might also be mistaken about the details.
If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)
@SimonSchroeder
The trouble is, changing one character in place is only one of the three possibilities the OP asked about. The other two do not keep the length the same. It is true this particular one could be done by opening ReadWrite, but IMHO encouraging this as the solution is not good/generic. We have had a lot of questions where people think it's fine to update a text file in ReadWrite mode, the sooner they learn the right way to do it for an arbitrary update the better!Besides, I didn't fancy explaining to the OP about the code needed to save the start-of-line offset, seek to it, update, then seek back to where he was and continue!
-
I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.
My claims are fully untested, so I might also be mistaken about the details.
If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)
@SimonSchroeder said in change/add letter in text file c++:
If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)
And that's why we did not suggest it - the op is unable to find QIODevice::readLine() by himself so your idea is way out of the possibilities of him.
Flipping an ascii character with QFile is also no problem - with QFile::map() it's a simple search'n'replace