1

I'm currently working on a binary file creation. Here is what I have tried.

Example 1:

#include<stdio.h>

int main() {
    /* Create the file */
    int a = 5;
    FILE *fp = fopen ("file.bin", "wb");
    if (fp == NULL)
      return -1;
    fwrite (&a, sizeof (a), 1, fp);
    fclose (fp);
    }

    return 0;
}

Example 2:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *fp;
    char str[256] = {'\0'};
    strcpy(str, "3aae71a74243fb7a2bb9b594c9ea3ab4");
    fp = fopen("file.bin", "wb");
    if(fp == NULL)
        return -1;
    fwrite(str, sizeof str, 1, fp);
    return 0;
}

Example 1 gives the right output in binary form. But Example 2 where I'm passing string doesn't give me right output. It writes the input string which I have given into the file and appends some data(binary form).

I don't understand and I'm unable to figure it out what mistake I'm doing.

4
  • Instead of "sizeof str" in the fwrite() call, you should use strlen( str ). Also, what does the file content look like, from "hexdump file.bin", for example? Commented Jun 12, 2015 at 18:41
  • Example 2 is doing the right thing. You wrote out ASCII values, which are binary representations of characters. So of course you'll see the string if you examine the file. After that, you'll also get whatever gobblety gook lies beyond those characters in memory since you wrote out 256 bytes, but your string is shorter than that. Commented Jun 12, 2015 at 18:41
  • Do not forget to close the file fclose(fp);. Commented Jun 12, 2015 at 18:54
  • 1
    Using functions for null-terminated strings like strcpy and strlen seems like a bad idea for binary data. Commented Jun 12, 2015 at 19:04

2 Answers 2

2

The problem is that sizeof str is 256, that is, the entire size of the locally declared character array. However, the data you are storing in it does not require all 256 characters. The result is that the write operation writes all the characters of the string plus whatever garbage happened to be in the character array already. Try the following line as a fix:

fwrite(str, strlen(str), 1, fp);
Sign up to request clarification or add additional context in comments.

Comments

0

C strings are null terminated, meaning that anything after the '\0' character must be ignored. If you read the file written by Example 2 into a str[256] and print it out using printf("%s", str), you would get the original string back with no extra characters, because null terminator would be read into the buffer as well, providing proper termination for the string.

The reason you get the extra "garbage" in the output is that fwrite does not interpret str[] array as a C string. It interprets it as a buffer of size 256. Text editors do not interpret null character as a terminator, so random characters from str get written to the file.

If you want the string written to the file to end at the last valid character, use strlen(str) for the size in the call of fwrite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.