1

I used the code below to open a binary file fp (the file contain a saved 2D array) and put it inside a pipe:

if ((fp=fopen("file", "rb"))==NULL) {
    printf("Cannot open file.\n");
}
if (fread(array, sizeof(int), 5*5, fp) != 5*5) {
    if (feof(fp))
        printf("Premature end of file.");
} else {
    printf("File read error fread.");
}

Is this the code to put it inside the pipe?

close(fd[0]);
if ((ch=fgetc(fp))==EOF)
    write(fd[1], &ch, 1 );

If I want to make a sum of the array, how could I make it?

8
  • You're still calling fread when the file open fails. You should return after error messages.
    – phihag
    Jan 9, 2011 at 13:02
  • @phihag- no error message in this code
    – Bobj-C
    Jan 9, 2011 at 13:04
  • @Bobj-C then what is the message in the second line? Looks like an error message to me. Anyway, you call fread with fp even if fp is NULL, don't you?
    – phihag
    Jan 9, 2011 at 13:11
  • the second message i dont know if it right because this i asked
    – Bobj-C
    Jan 9, 2011 at 13:20
  • 1
    @Bobj-C: No, you didn't ask about that part of the code (i.e., the message on the second line, not the second message!), you asked how to pipe the data and how to sum the array (what does the array hold?). @phihag is trying to point out that you are checking for a failed open and reporting it, but then continuing to use the file-descriptor anyway. Jan 9, 2011 at 13:57

1 Answer 1

2

The most sensible way to write the array to the pipe, as long as the size remains small, is to do:

int nw = 5 * 5 * sizeof(int);
if (write(fd[1], array, nw) != nw)
    err_exit("Failed to write to pipe");

(Where err_exit() is a function that writes a message to standard error and exits (or does not return.)

This assumes that your array is a 5x5 array (a comment from you implies it is 10x2, in which case your reading code has major problems). It assumes that the size of the buffer in a pipe is big enough to hold the data; if it is not, your write call may block. It assumes that there is somewhere a process to read from the pipe; if this is the only process, the write() will trigger a SIGPIPE signal, killing your process, because of the close(fd[0]);.

Writing one byte at a time is possible - it is not stellar for performance.

Reading one byte at a time from fp after you've already read the data into array is not entirely sensible - you are at best reading different data for writing to the pipe.

The normal way of summing a 2D array is (C99):

enum { DIM_1 = 5, DIM_2 = 5 };
int array[DIM_1][DIM_2];

...data to load array...

int sum = 0;
for (int i = 0; i < DIM_1; i++)
{
    for (int j = 0; j < DIM_2; j++)
         sum += array[i][j];
}

It doesn't matter where the data came from, just so long as you actually initialized it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.