I would like to know if there is a way in C
to overwrite an existing value that has already been printed instead of creating a new line every time or just movin
You're looking for a carriage return. In C, that's \r
. This will take the cursor back to the start of the current line without starting a new line (linefeed)
Printed where?
If your are outputting data to the standard output, you can't generally come back and change anything that has already been written. If your standard output is directed to terminal, you can try outputting \r
character that will move the cursor to the beginning of the line on some terminals (the effect is platform-dependent), so that the subsequent output line will overwrite what was printed in that line previously. This will produce a visual effect of old data being replaced with new data. However, this does not really "replace" the old data in the stream, meaning that if you redirect the standard output to file, the file will store everything that was printed. Keep in mind again, that \r
will force you overwrite the entire line on the terminal.
If you output your data to a file, then you can use fseek
function to go back to some previously visited point and "start over" from there, overwriting the data in the process.