473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

piping to c

Hello. I have to write a pipe to joint a GUI in perl and a simulation
program in c. I have read all the docs found in perl.com and now I am
trying to make a test with this simple program. It seems working but it
does not write "bonjour" at the end. Can anybody tell me where my
mistake is? I will be happy for any sample of code for a pipe between c
and perl sent to me too.
Thanks in advance,
Ignazio

#include <stdio.h>
#include <memory.h>
#include <unistd.h>

char chaine[7];
int status;

int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
int pid;
if ((pid = fork()) < 0)
{
printf("fork failed\n");
return 2;
}

if (pid == 0)
{
/* child */
close(pfd[0]);
write(pfd[1],"bonjour",7);
dup2(pfd[1], 1); /* connect the write side with stdout */

close(pfd[1]); /* close the write side */
printf("CHAINE FILS %s\n",chaine);
//return 3;
exit(0);
}
else
{
/* parent */
close(pfd[1]); /* close the unused write side */
dup2(pfd[0], 0); /* connect the read side with stdin */
read (pfd[0],chaine,7);

close(pfd[0]); /* close the read side */
printf("CHAINE PARENT %s\n",chaine);
wait(&status);
}
}

Jan 16 '07 #1
7 1812

"Igna" <ig*******@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
Non-standard stuff. LS

Jan 16 '07 #2
Igna <ig*******@gmail.comwrote:
Hello. I have to write a pipe to joint a GUI in perl and a simulation
program in c. I have read all the docs found in perl.com and now I am
trying to make a test with this simple program. It seems working but it
does not write "bonjour" at the end. Can anybody tell me where my
mistake is? I will be happy for any sample of code for a pipe between c
and perl sent to me too.
Sorry, but you ended up in the wrong place. fork(), dup2() etc.
aren't (standard) C functions but UNIX extensions and for this
reason off-topic here. You will find a much more receptive
audience for your problems in e.g. comp.unix.programmer.

<OT>
You don't even create a pipe using pipe(), so there's no pipe to
write to at all, just an uninitialized array of two integers. And
on top of that you first write to pfd[1] before the dup2() to make
it the standard output of the program. But for more details please
ask in comp.unix.programmer.
</OT>
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jan 16 '07 #3

Igna wrote:
Hello. I have to write a pipe to joint a GUI in perl and a simulation
program in c. I have read all the docs found in perl.com and now I am
trying to make a test with this simple program. It seems working but it
does not write "bonjour" at the end. Can anybody tell me where my
mistake is? I will be happy for any sample of code for a pipe between c
and perl sent to me too.
Thanks in advance,
Ignazio

#include <stdio.h>
#include <memory.h>
Non-standard header. Not used in code

#include <unistd.h>
Non-standard header. Implies POSIX/Unix code; followup in Unix
development forum

char chaine[7];
Uninitialized array - caution required.
int status;

int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
Uninitialized array - caution required.

>
int pid;
if ((pid = fork()) < 0)
Non-standard function fork() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)
{
printf("fork failed\n");
return 2;
Non-standard return value from main() - only 0, EXIT_SUCCESS and
EXIT_FAILURE are acceptable - value implies POSIX/Unix code; followup
in Unix development forum

}

if (pid == 0)
{
/* child */
close(pfd[0]);
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[0] has not been
initialized with any value
write(pfd[1],"bonjour",7);
Non-standard function write() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function write() - pfd[1] has not been
initialized with any value

dup2(pfd[1], 1); /* connect the write side with stdout */
Non-standard function dup2() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function dup2() - pfd[1] has not been
initialized with any value

close(pfd[1]); /* close the write side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[1] has not been
initialized with any value

printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - array chaine[] has not
been initialized with any value

//return 3;
exit(0);
}
else
{
/* parent */
close(pfd[1]); /* close the unused write side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[1] has not been
initialized with any value

dup2(pfd[0], 0); /* connect the read side with stdin */
Non-standard function dup2() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function dup2() - pfd[0] has not been
initialized with any value

read (pfd[0],chaine,7);
Non-standard function read() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function read() - pfd[0] has not been
initialized with any value
close(pfd[0]); /* close the read side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[0] has not been
initialized with any value

printf("CHAINE PARENT %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
wait(&status);
Non-standard function wait() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)

}
Caution: Missing return value from main() - only 0, EXIT_SUCCESS and
EXIT_FAILURE are acceptable
}
HTH
--
Lew

Jan 16 '07 #4
On Jan 16, 7:29 pm, "Lew Pitcher" <lpitc...@sympatico.cawrote:
<snip>
char chaine[7];
Uninitialized array - caution required.
<snip>
printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
Are you sure about these two? "chaine" is not automatic, it falls under
external linkage and thus (quotes from C89):
1. "An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration."
2. "If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant."
--
WYCIWYG - what you C is what you get

Jan 16 '07 #5

matevzb wrote:
On Jan 16, 7:29 pm, "Lew Pitcher" <lpitc...@sympatico.cawrote:
<snip>
char chaine[7];
Uninitialized array - caution required.
<snip>
printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
Are you sure about these two? "chaine" is not automatic, it falls under
external linkage and thus (quotes from C89):
1. "An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration."
2. "If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant."
You are correct. I goofed, and gave an incomplete (and thus inaccurate)
response.

I should have said
char chaine[7];
Array not explicitly initialized - caution required as implicit
initialization may not satisfy your later use of the array

[snip]
printf("CHAINE FILS %s\n",chaine);
Questionable argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with a printable string

--
Lew

Jan 16 '07 #6
Lew Pitcher wrote:
Igna wrote:
>char chaine[7];

Uninitialized array - caution required.
Not true. This array is declared outside of any function, so is
initialized to all zeros. Improper 'correction' - caution required.
Jan 16 '07 #7
"Lew Pitcher" <lp******@sympatico.cawrites:
matevzb wrote:
[snip]
You are correct. I goofed, and gave an incomplete (and thus inaccurate)
response.

I should have said
>char chaine[7];
Array not explicitly initialized - caution required as implicit
initialization may not satisfy your later use of the array

[snip]
> printf("CHAINE FILS %s\n",chaine);
Questionable argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with a printable string
If chaine[] has not been altered, it *does* contain a printable
string. It happens to be "".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Csaba Henk | last post by:
For me, one reason for using Python is that with the help of it I can rid of several problems that I faced with during writing scripts in Bourneish shells. Biggest of these problem was the...
2
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use...
4
by: christopher diggins | last post by:
I just wanted to share this technique for piping the cout from one function to the cin of another, using the pipe operator: #include <iostream> #include <sstream> using namespace std; ...
3
by: Russ Schneider | last post by:
Is there an easy way to pipe a select statement's output to a file? -- http://www.sugapablo.com <--music ] http://www.sugapablo.net <--personal ] sugapablo@12jabber.com <--jabber IM ]...
1
by: saibotorama | last post by:
What is the Python translation for this Bash statement: tar cf - "${files}" | bzip2 "$file".tar.bz2 (Ignoring the fact that "tar cjf" also exists...) In other words, how does one pipe...
2
by: moti | last post by:
I am a newbie to MySQL. I'm confused as to the piping syntax of SERVER in the User DSN in ODBC for a client PC. If I use an ip it does not work, probably because of a firewall. So I tried using...
3
by: noob2008 | last post by:
can anyone explain me what is piping in C++ programming? for example invoice.exe < purchase.dat > statement.txt do i replace that where all the cin statements? i created a text file called...
1
by: AtoZ | last post by:
Hello, I'm looking for some experience translation between the software PDMS to Pro/ENGINEER ( Pro/PIPING ) and reverse. If some engineer know the solutions, their feedback will be very usefull...
4
by: samit | last post by:
I keen to persue coding like yahoo piping system which will include drag and drop and piping features
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.