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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:31:50+00:00 2026-06-14T07:31:50+00:00

I wrote this binary reader after a tutorial on the internet. (I’m trying to

  • 0

I wrote this binary reader after a tutorial on the internet. (I’m trying to find the link…)

The code reads the file byte by byte and the first 4 bytes are together the magic word. (Let’s say MAGI!) My code looks like this:

std::ifstream in(fileName, std::ios::in | std::ios::binary);
char *magic = new char[4];

while( !in.eof() ){
   // read the first 4 bytes
   for (int i=0; i<4; i++){
      in.get(magic[i]);
   }

   // compare it with the magic word "MAGI"
   if (strcmp(magic, "MAGI") != 0){
        std::cerr << "Something is wrong with the magic word: " 
                  << magic << ", couldn't read the file further! " 
                  << std::endl; 
        exit(1);
    }

   // read the rest ...
}

Now here comes the problem, when I open my file, I get this error output:
Something is wrong with the magic word: MAGI?, couldn't read the file further! So there is always one (mostly random) character after the word MAGI, like in this example the character ?!
I do think that it has something to do with how a string in C++ is stored and compared with each other. Am I right and how can I avoid this?

PS: this implementation is included in another program and works totally fine … weird.

  • 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-14T07:31:51+00:00Added an answer on June 14, 2026 at 7:31 am

    strcmp assumes that both strings are nul-terminated (end with a nul-character). When you want to compare strings which are not terminated, like in this case, you need to use strncmp and tell it how many characters to compare (4 in this case).

    if (strncmp(magic, "MAGI", 4) != 0){
    

    When you try to use strcmp to compare not null-terminated char arrays, it can’t tell how long the arrays are (you can’t tell the length of an array in C/C++ just by looking at the array itself – you need to know the length it was allocated with. The standard library is not exempt from this limitation). So it reads any data which happens to be stored in memory after the char array until it hits a 0-byte.

    By the way: Note the comment to your question by Lightness Races in Orbit, which is unrelated to the issue you are having now, but which hints a different bug which might cause you some problems later on.

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

Sidebar

Related Questions

I'm trying to read from a binary file with BinaryReader . This is how
I have to write an object in to binary file.My struct looks like this.
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote this infeasible oml by mistake while trying to do another thing. What's
i'm trying to read a binary file (for example an executable) into a string,
I wrote this function that uses a binary search to look for a specific
I am trying to make a program that writes the binary code of a
I wrote the below method to archive files into one file using binary mode:
I'm trying to serialize/deserialize string. Using the code: private byte[] StrToBytes(string str) { BinaryFormatter

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.