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 7077447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:25:28+00:00 2026-05-28T06:25:28+00:00

I am creating a file with the content depending on some local variables. I

  • 0

I am creating a file with the content depending on some local variables. I don’t know how long the file will be at the end. Since I define a buffer[] array with a fixed size, the last characters in the generated file are garbage. How should I trim buffer[] array before writing it to the file? Or fwrite only the used bytes?

Here is my code:

FILE * pFile = NULL;
char buffer[300];
int ret_val = -1;
strcpy_s(buffer, "The path is \"");
strcat_s(buffer, pathVariable);
strcat_s(buffer, "\"\n");
pFile = fopen(myfile, "w");
if(NULL == pFile)
{
  perror("File opening error");
  return -1;
}
ret_val = fwrite(buffer, 1, sizeof(buffer), pFile );
if(sizeof(buffer) != ret_val)
{
  perror("File writing error");
  return -1;
}
fclose(pFile);
  • 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-28T06:25:29+00:00Added an answer on May 28, 2026 at 6:25 am

    fwrite() doesn’t know which bytes you consider “used”. When you call it like so:

    fwrite(buffer, 1, sizeof(buffer), pFile );
    

    it will write sizeof(buffer) bytes.

    Change the call to:

    ret_val = fwrite(buffer, 1, used_bytes, pFile );
    

    where used_bytes is some variable that you set before the call.

    You’ll also need to alter the subsequent error check:

    if(ret_val != used_bytes)
    

    edit In this case, it would appear you’d want to set used_bytes to strlen(buffer).

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

Sidebar

Related Questions

I am creating one text file which will connected to some server. this text
I've been wrangling with creating my own File Browser Content Provider for Telerik's FileExplorer
I am creating a file-oriented database of some test results performed by various users.
I have a function creating a file for download like so: header(Content-type: application/octet-stream); header(Content-disposition:
I am creating a Javascript file that will be embedded onto a 3rd party
I am using XML file for creating Context Menu for my ListView. (Please see
While creating a file synchronization program in C# I tried to make a method
What are the best practices around creating flat file database structures in PHP? A
Do I need to malloc when creating a file to write to? The file
I've got the following in my .css file creating a little image next to

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.