LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2015, 08:37 AM   #1
linuxok
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 12-07-2015, 09:06 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You could try functions fopen+fwrite+fread+fclose, with checking/logging the return values after every function-call.
 
Old 12-07-2015, 07:22 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
If, for example, file was not successfully opened, it is entirely possible that the subsequent attempt to write to it silently failed . . .
 
Old 12-08-2015, 06:21 AM   #4
linuxok
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
NevemTeve,
I prefer to use fstream.

sundialsvcs,
file was opened correctly, at least is_open() call gives true result.
 
1 members found this post helpful.
Old 12-08-2015, 04:09 PM   #5
linuxok
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
This is the example which shows the problem:
http://pastebin.com/gJ4PbHvy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to store multiple sed operations using pipe in a variable amitverma4587 Programming 2 06-02-2011 10:58 AM
[SOLVED] perl fork & pipe help kdelover Programming 5 04-09-2011 08:42 AM
pipe and fork Trouble ajb181 Linux - Embedded & Single-board computer 2 10-01-2009 09:04 PM
Wierd behaviour using fork() and pipe() cybermix Programming 4 01-08-2004 03:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration