change/add letter in text file c++
-
@RuWex said in change/add letter in text file c++:
Do you know how to do it?
Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one.
@Christian-Ehrlicher said in change/add letter in text file c++:
@RuWex said in change/add letter in text file c++:
Do you know how to do it?
Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one.
this is the only way??
there are not a way to write to the file? -
@Christian-Ehrlicher said in change/add letter in text file c++:
@RuWex said in change/add letter in text file c++:
Do you know how to do it?
Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one.
this is the only way??
there are not a way to write to the file?@RuWex said in change/add letter in text file c++:
there are not a way to write to the file?
You can also write to a file as we told you.
-
@Christian-Ehrlicher said in change/add letter in text file c++:
@RuWex said in change/add letter in text file c++:
Do you know how to do it?
Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one.
this is the only way??
there are not a way to write to the file?@RuWex said in change/add letter in text file c++:
there are not a way to write to the file?
You can't insert into a file! You can only overwrite what is inside or append at the end of the file. That's why @Christian-Ehrlicher suggested to write the output to a new file and when you're done delete the old file and rename the new so it has the name of the original file.
-
@Christian-Ehrlicher said in change/add letter in text file c++:
@RuWex said in change/add letter in text file c++:
Do you know how to do it?
Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one.
this is the only way??
there are not a way to write to the file? -
@RuWex said in change/add letter in text file c++:
there are not a way to write to the file?
In a word, you cannot write back to the same file as you are reading from at the same time, it would mess up the data you are reading.
@JonB
that is the point!!
I have a function that i am reading the file and something and every line i send to another function in order to check if it begins with # (the function get the line and pointer)
and then I want that the function will continue with the fixes file
its really important for me -
@JonB
that is the point!!
I have a function that i am reading the file and something and every line i send to another function in order to check if it begins with # (the function get the line and pointer)
and then I want that the function will continue with the fixes file
its really important for me@RuWex said in change/add letter in text file c++:
and then I want that the function will continue with the fixes file
Don't know what this mean, but did you actually read what @Christian-Ehrlicher wrote?
-
@JonB
that is the point!!
I have a function that i am reading the file and something and every line i send to another function in order to check if it begins with # (the function get the line and pointer)
and then I want that the function will continue with the fixes file
its really important for me@RuWex said in change/add letter in text file c++:
its really important for me
What is the relevance of this? We have told you what you need to do and that you cannot write back to the text file while reading from it. Just because something else might be "important" to you does not mean it can be done or you can ignore how things work/don't work!
-
@RuWex said in change/add letter in text file c++:
and then I want that the function will continue with the fixes file
Don't know what this mean, but did you actually read what @Christian-Ehrlicher wrote?
-
@RuWex This is what @Christian-Ehrlicher wrote:
"Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one."
Did you read it?!
It was already explained in this thread that you CAN'T read AND write into the same file in a meaningful way. And it was also explained to you that you need to write to a temporary file first and rename it if done. Did you read that? -
@RuWex said in change/add letter in text file c++:
but there are not a easier and nicer way?
If the file is not too big you can keep the lines you read in memory until you're done with reading the file and modifying its content. Then write all this lines at once into the old file (after closing and reopening it for writing).
-
@RuWex This is what @Christian-Ehrlicher wrote:
"Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one."
Did you read it?!
It was already explained in this thread that you CAN'T read AND write into the same file in a meaningful way. And it was also explained to you that you need to write to a temporary file first and rename it if done. Did you read that? -
@RuWex
If your file is small you could read it all into memory (splitting into lines for an array), make your changes in memory, and then overwrite the original file in one go. That would avoid the need for two files. But it does not alter the fact that you cannot write to/overwrite the file until you have finished reading from it completely.... -
@RuWex
If your file is small you could read it all into memory (splitting into lines for an array), make your changes in memory, and then overwrite the original file in one go. That would avoid the need for two files. But it does not alter the fact that you cannot write to/overwrite the file until you have finished reading from it completely.... -
@JonB but I can do it in te middle that the
readfile()- read a text fule and send to the changetextfile() function each line
so can I do it every time I found # IN CHANGETEXTFILE? -
@JonB but I can do it in te middle that the
readfile()- read a text fule and send to the changetextfile() function each line
so can I do it every time I found # IN CHANGETEXTFILE?@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.
-
@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 ;-)