LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Fork, pipe and file operations (https://www.linuxquestions.org/questions/programming-9/fork-pipe-and-file-operations-4175560871/)

linuxok 12-07-2015 08:37 AM

Fork, pipe and file operations
 
I have got pipe ( http://linux.die.net/man/2/pipe ) to enable communication between 2 processes in forked program. Everything goes right until I want to perform some file operations.

This code works:
Code:

pipe.writeBuffer(message.c_str(), message.length());
ofstream file;
file.open(name.c_str(), ios::app);
file << someStream;    // put some data to file (many times)

But this one not:
Code:

ofstream file;
file.open(name.c_str(), ios::app);
pipe.writeBuffer(message.c_str(), message.length());
file << someStream;    // put some data to file (many times)

In the second example there is no effect of instruction "file << someStream" - empty file.
What is wrong with that? Is it a problem with file descriptor? Pipe uses fd[0] - input and fd[1] - output.

NevemTeve 12-07-2015 09:06 AM

You could try functions fopen+fwrite+fread+fclose, with checking/logging the return values after every function-call.

sundialsvcs 12-07-2015 07:22 PM

If, for example, file was not successfully opened, it is entirely possible that the subsequent attempt to write to it silently failed . . .

linuxok 12-08-2015 06:21 AM

NevemTeve,
I prefer to use fstream.

sundialsvcs,
file was opened correctly, at least is_open() call gives true result.

linuxok 12-08-2015 04:09 PM

This is the example which shows the problem:
http://pastebin.com/gJ4PbHvy


All times are GMT -5. The time now is 03:58 PM.