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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:20:51+00:00 2026-06-12T16:20:51+00:00

I have a binary file that I open, modify, and close. And then I

  • 0

I have a binary file that I open, modify, and close. And then I printf to the console.

This all works fine, but I realized just now that it’s appending whatever I’m printing to the console to the end of the binary file, and it makes no difference whether or not the file is open or closed.

The same thing happens with fprintf.

What’s going on here? Is there something I don’t understand about file I/O?

Update: Here’s the code:

FILE *out = fopen("test","wb+");
fseek(out,0,SEEK_END);
fwrite("test",1,10,out);
fwrite("test",1,10,out);
fwrite("test",1,10,out);
int pos = ftell(out);
fwrite(&pos,sizeof(int),1,out);
fclose(out);
fprintf(stdout,"%s","hello");
  • 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-06-12T16:20:52+00:00Added an answer on June 12, 2026 at 4:20 pm

    The calls to fwrite() are incorrect as they are instructing fwrite() to write 10 characters from a 5 character array (string literals have an implicit null character appended). This will be accessing beyond the ends of the array, resulting in undefined behaviour and is a probable cause of the strange behaviour.

    Correct the fwrite() calls:

    fwrite("test", 1, 4, out);
    

    As per comment, if there must be 10 characters then declare an array:

    char msg[10] = "test"; /* Unspecified elements will be null. */
    
    fwrite(msg, 1, sizeof(msg), out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary file that contains blocks of information ( I'll refer to
I have a binary file that I would like to regex search/replace hex bytes
I have a binary file, that was written in C, which contains a long
I have a function that copies binary file public static void copyFile(String Src, String
I have a raw image file that is saved in binary data (no encoding).
I have response stream from a ftp web request that returns binary file. I
i have a binary file which has 4 KB of header information and then
I have a program that opens a large binary file, appends a small amount
I have an ASP routine that gets a binary file's contents and writes it
I have 2 applications. One in C++ (windows) open a binary file and only

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.