1

I would like to create a file and write "A" into it (ascii is 65 == 01000001). Strange fact, whatever the value of std::string binary, there is always writed the letter P in myfile.txt.

    std::string binary = "01000001";
    std::string file = "myfile.txt";
    FILE* f;
    f = fopen(file.c_str(), "wb");      
    fwrite(&binary, 1, 1, f);     
    fclose(f);

After execution of this code, I read binary data with the command xxd -b myfile and I get this :

00000000: 01010000

Do you see a problem on this code ?

9
  • 1
    Why not use C-style strings directly? (Joke - you're not using C++ streams) You did fopen(file.c_str()..., but did fwrite(&binary.... One of them is wrong. Commented Jan 13, 2017 at 12:39
  • 1
    Do you think it could be easier to work with ofstream ? Commented Jan 13, 2017 at 12:52
  • The binary variable is a string that contains the ASCII representation of "01000001". This is not a number is TEXT. To write "A" into the file you should use binary = "A" and then `fwrite(binary.c_str(),1,1,f)!!! But I think you would convert binary in its ASCII value starting from the binary value. Commented Jan 13, 2017 at 12:53
  • This code is a cocktail between C and C++, but solves your problem: #include <iostream> #include <cstdio> #include <cstdlib> int main(void) { std::string binary = "01000001"; std::string file = "myfile.txt"; char c; FILE* f; c=strtol(binary.c_str(), NULL, 2); f = fopen(file.c_str(), "wb"); // fwrite(binary.c_str(),1, 1, f); fwrite(&c,1, 1, f); fclose(f); return 0; } ... You shall insert the right indentation! Commented Jan 13, 2017 at 12:55
  • My goal is to copy files (not only text files), I have to use binary. I guess std::string isn't the best way to do that. Do you have suggestions to do that without strings ? Commented Jan 13, 2017 at 12:59

2 Answers 2

6
fwrite(&binary, 1, 1, f);

You pass a pointer of the std::string to fwrite, that's bad.

You will want to get a pointer to the internal buffer of the string by calling c_str():

fwrite(binary.c_str(), 1, 1, f);  

This is another reason to not use C file handles, fwrite's first argument is a const void* which is why your compiler didn't give you an error in the first place.

Sign up to request clarification or add additional context in comments.

1 Comment

With his current code, this will write the character '0', not 'A' as he says he wants to write.
2

Strings are bad when you want to do a bitstream...

int8_t binary = 0b01000001;
std::string file = "myfile.txt";
FILE* f;
f = fopen(file.c_str(), "wb");      
fwrite(&binary, 1, 1, f);     
fclose(f);

But when you are using C++, you don't really need to use C libraries

#include <fstream>

int main()
{
    std::fstream f;
    std::string file = "myfile.txt";
    f.open(file, std::fstream::out | std::fstream::binary);
    int8_t binary = 0b01000001; // int8_t is required to only write 8 bits/1 byte
    f << binary;
    f.close();
    return 0;
}

1 Comment

Your fwrite call should be passed &binary instead of binary

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.