Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:00:25+00:00 2026-05-19T02:00:25+00:00

I used the code below to open a binary file fp (the file contain

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:00:25+00:00Added an answer on May 19, 2026 at 2:00 am

    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 5×5 array (a comment from you implies it is 10×2, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the code below to compress files and they keep growing instead of
The code below shows a sample that I've used recently to explain the different
In my ASP.NET application,When I try to open PDF file by using the below
I've seen (and used) code to have a link spawn a javascript action many
Info - for better formatting, I used code-formatting throughout the whole posting. Hi, I
Which programming language(s) were used to code Windows Vista?
As per my understanding stateless session beans are used to code the business logic.
I'm currently refactoring/tidying up some old C code used in a C++ project, and
Is there a way to find the number of lines of code used in
In our code we used to have something like this: *(controller->bigstruct) = ( struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.