1

I have a function two functions, writing and reading from the binary file in C++.

void save_ascII(string ascII_text, string filename)
{
    ofstream fout;

    fout.open(filename,ofstream::binary);
    
    size_t input_size = ascII_text.size();
    if (fout.is_open())
    {
        fout.write(reinterpret_cast<char*>(&input_size), sizeof(input_size));
        fout.write(reinterpret_cast<char*>(&input_size), input_size);
        fout.close();
    }
}

void read_ascII(string filename)
{
    string read_input;

    ifstream fin(filename,fstream::binary);
    
    size_t read_size;

    if (fin.is_open())
    {
        fin.read(reinterpret_cast<char*>(&read_size), sizeof(read_size));
        read_input.resize(read_size);
        fin.read(&read_input[0], read_size);
        fin.close();
    }
}

The problem is that when it reads from the binary, it just dummy data on the memory.

When it reads from the binary file, it shows: 
►╠╠╠╠╠╠╠╠▄²A

Any suggestion really appreciates it.

4
  • 2
    Provide some minimal reproducible example (with a main) in your question, and show an example of input file. BTW, in 2021, you should use UTF-8 everywhere. Notice that a good example of binary files are executables. On Linux e.g. Debian they use the ELF format Commented Feb 12, 2021 at 5:35
  • If you use GCC as your compiler, invoke it as g++ -Wall -Wextra -g to get warnings and debug information, then use the GDB debugger to understand the behavior of your program. You need first to document (e.g. in English, perhaps using EBNF notation) the format of your input files. Commented Feb 12, 2021 at 5:40
  • And fyi, fstream::out|fstream::binary and fstream::in|fstream::binary should be your defacto read masks. I noticed you never bothered to check that the read actually succeeded. Nor did you mention whether you examined the file via hex dump to ensure the content was really there. If you do, you'll notice you're writing the length twice and completely ignoring the actual string content on output. Cut+paste can be cruel sometimes, eh? Commented Feb 12, 2021 at 5:41
  • @WhozCraig I am so sorry about that. This is my first time dealing with binary files. I have absolutely no idea what I am doing. Thank you for pointing it out, now I know what to do next. Commented Feb 12, 2021 at 5:52

1 Answer 1

3

In your code,

size_t input_size = ascII_text.size();
if (fout.is_open())
{
    fout.write(reinterpret_cast<char*>(&input_size), sizeof(input_size));
    fout.write(reinterpret_cast<char*>(&input_size), input_size); //<--bug
    fout.close();
}

instead of the line marked with bug<--, you need

fout.write(ascII_text.data(), input_size);
Sign up to request clarification or add additional context in comments.

2 Comments

This actually works but it just typically writes the character to the file instead of in the binary form. It works pretty same as with c.str().
There is no difference between "characters" and "binary form" here. There is only one way to write characters to a file - just copy the memory occupied by them to the file - the same for "text" or "binary" file. Note that ofstream::binary does something different - it prevents converting '\n' into '\r','\n'. For numbers "binary" means writing memory contents directly to the file, while "text" means format conversion to the decimal presentation.

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.