Hi, I'm new to the site. I have a problem that I'm having trouble tackling. I need to modify an existing text file by replacing the first character of every third line, which happens to be a space, with the number 0. The text file is several thousand lines, so it's much too long to do by hand. Could someone provide me with some input on how to do this?
#include <headers>
int main()
{
-> declare three strings:
in_filename,
out_filename,
current_line
-> also declare an int:
line_counter=1
-> get filenames using getline
-> open one file for input (fin)
-> and the other for output (fout)
while (getline(fin, current_line))
{
if (!(line_counter++%3))
current_line[0]='0';
fout << current_line << std::endl;
}
return 0;
}
I really have no idea what the internal mechanism is (hence the problem). But then, reading a byte and then overwriting the same byte seems like it should be a trivial task..
if all we are talking about is memory, that's true (that it's trivial)
however, here, we have to coordinate with an underlying FILE* which probably has its own buffering mechanisms optimized for either reading or writing
with arbitrary read/write interleaving, even trivial concepts such as EOF could become ambiguous if you are not careful (is EOF what's really on disk or what's in memory? have you flushed your write yet?)
although I have never tried it, I believe that with binary files, doing arbitrary reads and writes may be more tractable - I can imagine a very large file with several threads reading and writing to such a file, as long as the flushing is synchronized, but even then, maybe you are limited to many readers and one writer