Hello all,

I am trying to understand the concept of pipe-ing input/output through a C program. I wrote this simple code:

#include <stdio.h>
#include <stdlib.h>

void in_read (int *foo) {
        read (0, foo, 1);
}

void in_write (int foo) {
        write (1, &foo, 1);
}

int main (int argc, char **argv) {
        int y;
        if (argc != 2) {
                in_read (&y);
                printf ("Y is %d\n", y);
        }
        else {
                y = atoi(argv[1]);
                in_write(y);
        }
        return EXIT_SUCCESS;
}

When I run it with ./a.out 14 | ./a.out , this produces the expected result of 14.

However, my desire is to take the argv outputs and use it as pipe input to populate data in a struct. Thus, I wrote this more complex code:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
        int h;
        int w;
} box;

void box_read (int *foo) {
        read (0, foo, 1);
}

void box_write (int foo) {
        write (1, &foo, 1);
}

void box_read_help (box *foo) {
        box_read (&foo->h);
        box_read (&foo->w);

}

void box_write_help (box *foo) {
        box_write (foo->h);
        box_write (foo->w);
}

int main (int argc, char **argv) {
        box mybox;
        if (argc != 3)  {
                box_read_help (&mybox);
                printf ("Height: %d\n", mybox.h);
                printf ("Width: %d\n", mybox.w);
        }
        else {
                mybox.h = atoi(argv[1]);
                mybox.w = atoi(argv[2]);
                box_write_help (&mybox);
        }
        return EXIT_SUCCESS;
}

And try to run it with ./a.out 16 14 | ./a.out and do not receive expected output.

The caveat is I can not modify the main() function or the box_read() and box_write() functions-I am only allowed to modify the box_read_help() and the box_write_help() I believe my error is in the box_read_help function, can anyone help me out?

Thanks.

What is the expected result? I get

$ ./a.out 16 14 | ./a.out
Height: 16
Width: 14
$

When you say pipe, I think of http://linux.die.net/man/2/pipe. You are using the pipe-ing of the shell and reading/writing to standard in and out. Your program is not making a call to the function pipe() and reading and writing to that file descriptor.
Is that what you want to do?

What is the expected result? I get

$ ./a.out 16 14 | ./a.out
Height: 16
Width: 14
$

When you say pipe, I think of http://linux.die.net/man/2/pipe. You are using the pipe-ing of the shell and reading/writing to standard in and out. Your program is not making a call to the function pipe() and reading and writing to that file descriptor.
Is that what you want to do?

That is the expected result. What compiler are you using? I am using gcc 4.4.3 under Ubuntu 10.04.

The height value gives something that I think is the lower integer bound, and the width always gives 32526. Hmm

gcc version 4.2.1 (Apple Inc. build 5664). I am on a Mac as you can see.

I think I know what it is. Do this:

$ ./a.out 16 14 > foo.txt
$ od -tx1 foo.txt
0000000    10  0e  <=== Not what you want                                                      
0000002

Try changing your code to this

void box_read (int *foo) {
        read (0, foo, 4);
}

void box_write (int foo) {
        write (1, &foo, 4);
}

Notice the 1 is now a 4. Now it get

$ ./a.out 16 14 > foo.txt 
$ od -tx1 foo.txt
0000000    10  00  00  00  0e  00  00  00    <=== This is what you want                            
0000010
$

I'm going to guess that it is an endian issue. What do you get when you change the 1 to 4?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.