![]() |
|
#1
|
||||
|
||||
File copy using pipes - inter process communicationHello folks,
Im trying to create a simple program that allows a process to create a copy of a certain file. The parent reads from the file, sends the data over a pipe to child process. the child then reads data from the pipe. It creates a file and dumps the data that it read into this newly created file. sounds simple. But it has given me a good amount of headache. Before we see the code, lets see the issues that i face. I have been instructed to stick to either the fopen, fwrite etc calls or to the more low level open and write system APIs. I have decided to stick to the latter ones. nonetheless, I still have to find a graceful way to use read and write on regular files (not FIFOs or pipes) such that I can terminate the transfers on encountering EOF. Besides that, the real issues that I have been facing are these: 1.I am able to send the data across as long as it is less that the size of the intermediate buffers. But on the receiving end, which in my case is the child process I cannot write it in a file. 2.As of now (in the code below), I cannot even write to the pipe.. This is strange as I do receive the data in the child process as I verify that in the code itself. The file does get created, but file permissions are locked. And then the files contains nothing (0 bytes). Enough words. Time for the real deal here CPP / C++ / C Code:
So summarizing, the code above
All in all, heres the output i see on my console: Code:
Im confused. ...helppppppp!!!!!!!!!!!!!!__________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
||||
|
#2
|
|||
|
|||
Re: file copy using pipes - inter process communicationQuote:
CPP / C++ / C Code:
Now to get to some of your issues: Quote:
Then use something like the following to open the file for writing: CPP / C++ / C Code:
CPP / C++ / C Code:
I have put a couple of comments in your code in hopes of showing why you get the error trying to write to the pipe. By commenting out the exit(0) at the end of the child part of the program, it continues on into the parent's code and tries to write to the pipe after it has been closed. Quote:
A summary: CPP / C++ / C Code:
Maybe the following is what you had in mind when you commented out the exit(0) in the child process. CPP / C++ / C Code:
At any rate, you can't let the child process continue into the parent's code to try to write to the pipe. Regards, Dave Footnote: Many of the functions and headers are not part of the C standard library. I am assuming that you are using some GNU-compatible compiler. The modified program that I show works for me with GNU gcc on Linux (Centos 5) and with cygwin/gcc on Windows xp. |
|
#3
|
|||
|
|||
Re: File copy using pipes - inter process communicationDear Friend,
I have written the following code. This is working fine. This code also used pipe only to copy the content from one file to another file. CPP / C++ / C Code:
Last edited by admin : 01-Mar-2010 at 09:46.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: File copy using pipes - inter process communicationQuote:
I would like to know what compiler and operating system you are using. Did you really test the code that you posted? Did it really copy a text file to another file? (There are other errors that I believe would not let it copy correctly even if you fix the "else" problem.) Note that the changes that I suggested to the Original Poster will not make the code function the way that he wanted. I just wanted to try to answer his question about why his code was giving the "can not write to pipe" error, and to show how to get past that point. A complete example that works for binary files as well as text files could look something like CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 27-Feb-2010 at 00:32.
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Power Calibration Error In Nero Fix (hopefully) | matt3678 | Computer Hardware Forum | 60 | 20-Aug-2009 05:04 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 13:30 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 20:03 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 21:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The