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

  • SEARCH
  • Home
  • 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 6948027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:46:09+00:00 2026-05-27T13:46:09+00:00

I find fwrite fails when I am trying to write somewhat big data as

  • 0

I find fwrite fails when I am trying to write somewhat big data as in the following code.

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

int main(int argc, char* argv[])
{
    int size = atoi(argv[1]);
    printf("%d\n", size);
    FILE* fp = fopen("test", "wb");
    char* c = "";
    int i = fwrite(c, size, 1, fp);
    fclose(fp);
    printf("%d\n", i);
    return 0;
}

The code is compiled into binary tw
When I try ./tw 10000 it works well. But when I try something like ./tw 12000 it fails.(fwrite() returns 0 instead of 1)
What’s the reason of that? In what way can I avoid this?

EDIT: When I do fwrite(c, 1, size, fp) it returns 8192 instead of larger size I give.

2nd EDIT: When I write a loop that runs for size times, and fwrite(c, 1, 1, fp) each time, it work perfectly OK.
It seems when size is too large(as in the first EDIT) it only writes about 8192 bytes.
I guess something has limited fwrite write up to fixed size bytes at a time.

3rd EDIT: The above is not clear.
The following fails for space - w_result != 0 when space is large, where space is determined by me and w_result is object written in total.

w_result = 0;
char* empty = malloc(BLOCKSIZE * size(char));
w_result = fwrite(empty, BLOCKSIZE, space, fp);
printf("%d lost\n", space - w_result); 

While this works OK.

w_result = 0;
char* empty = malloc(BLOCKSIZE * sizeof(char)); 
for(i = 0; i < space; i ++)
    w_result += fwrite(empty, BLOCKSIZE, 1, fp);
printf("%d lost\n", space - w_result);

(every variable has been declared.)

I corrected some errors the answers memtioned. But the first one should work according to you.

  • 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-27T13:46:09+00:00Added an answer on May 27, 2026 at 1:46 pm

    With fwrite(c, size, 1, fp); you state that fwrite should write 1 item that is size big , big out of the buffer c.

    c is just a pointer to an empty string. It has a size of 1. When you tell fwrite to go look for more data than 1 byte in c , you get undefined behavior. You cannot fwrite more than 1 byte from c.

    (undefined behavior means anything could happen, it could appear to work fine when you try with a size of 10000 and not with a size of 12000. The implementation dependent reason for that is likely that there is some memory available, perhaps the stack, starting at c and 10000 bytes forward, but at e.g. 11000 there is no memory and you get a segfault)

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

Sidebar

Related Questions

Is the following program a valid C program? #include <stdio.h> int main() { fwrite(x,
All I can find using fopen() and fwrite() in C is to delete all
I have the following struct: struct Records { int Number; char Name[20]; float Salary;
It took me FOREVER to finally find code that can download a doc file
I want to write multiple structures to a single file. I used fwrite function
I was testing the access to the Internic WHOIS with the following code: $domains
I've been trying to create some php code that would let me take input
I need to lock the file, read the data, write to the file and
I want to find low-level C/C++ APIs, equivalent with write in linux systems, that
I have a configuration file like the following, NameA=3 NameB=2 NameC=1 The following code

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.