tags:

views:

30

answers:

1

I'm writing a application that acts like a filter: it reads input from a file (stdin), processes, and write output to another file (stdout). The input file is completely read before the application starts to write the output file.

Since I'm using stdin and stdout, I can run is like this:

$ ./myprog <file1.txt >file2.txt

It works file, but if I try to use the same file as input and output (that is: read from a file, and write to the same file), like this:

$ ./myprog <file.txt >file.txt

it cleans file.txtbefore the program has the chance to read it.

There's any way I can do something like this in a command line in Unix?

+1  A: 

The shell is what clobbers your output file, as it's preparing the output filehandles before executing your program. There's no way to make your program read the input before the shell clobbers the file in a single shell command line.

You need to use two commands, either moving or copying the file before reading it:

mv file.txt filecopy.txt
./myprog < filecopy.txt > file.txt

Or else outputting to a copy and then replacing the original:

./myprog < file.txt > filecopy.txt
mv filecopy.txt file.txt

If you can't do that, then you need to pass the filename to your program, which opens the file in read/write mode, and handles all the I/O internally.

./myprog file.txt                 # reads and writes according to its own rules
Bill Karwin