Modify every 3rd line of a text file

May 27, 2011 at 7:51pm
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?
May 27, 2011 at 9:37pm
#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;
}

Useful links:

http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/string/getline/
http://cplusplus.com/doc/tutorial/files/
Last edited on May 27, 2011 at 9:56pm
May 28, 2011 at 9:38am
Say, master roshi, why do you need to copy the file? I don't see a reason. But then I tried and I failed.. What am I missing?
May 28, 2011 at 9:55am
awk '{ if (NR%3==0) { printf "0%s\n",substr($0,2,length($0)-1); } else { print $0; } }' < input.txt > output.txt

input.txt
sdakfja2834732
13471234jkdsfj
238472398423r3
32938uyr328rji
23jek3j2ejk3j2
23jeij23kej3ke
2j3ekj32kejk3j
j2e3jdchjcsdhc
sdhfjdhsfjhdjs
sjdhfjdhsfjhjh

output.txt
sdakfja2834732
13471234jkdsfj
038472398423r3
32938uyr328rji
23jek3j2ejk3j2
03jeij23kej3ke
2j3ekj32kejk3j
j2e3jdchjcsdhc
0dhfjdhsfjhdjs
sjdhfjdhsfjhjh
May 28, 2011 at 11:48am
hamsterman wrote:
Say, master roshi, why do you need to copy the file?

I don't know. I thought it's easier this way.

hamsterman wrote:
But then I tried and I failed.. What am I missing?

Could you post your code?
May 28, 2011 at 4:20pm
1
2
3
4
5
6
7
8
9
#include <fstream>

int main(){
   std::fstream file("hello.txt");
   file.put('0');
   while(file) file.get();
   file.close();
   return 0;
}
seems to be enough to mess up the file. I get "0ĶĶĶĶĶĶĶ..." though if I remove line 5 or 6 it behaves normally..
May 28, 2011 at 11:58pm
hmm... ...even though fstream() defaults to mode fstream::in | fstream::out, I'm not sure you can alternate write and read like that

I mean, underneath, what would the OS be doing (in terms of flushing)?

Where would the underlying FPTR be?
May 29, 2011 at 8:05am
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..
May 31, 2011 at 10:00am
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
May 31, 2011 at 11:06am
Do you think that this could be more easily solved by directly using winapi functions?
May 31, 2011 at 11:19am
The stream has a 'seekg()' function for reading and 'seekp()' for writing. So it looks like reading/writing is independend. But I didn't try.
Topic archived. No new replies allowed.