Home > other >  Why is this not working? (Trying to convert a text file into a binary file)
Why is this not working? (Trying to convert a text file into a binary file)

Time:11-18

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include <sstream>
#include<iomanip>



using namespace std;
const string binaryfile = ".../binaryfile.dat";
void to_binary(const string& filename)
{
    ifstream ist(filename);
    ofstream ost(binaryfile, ios::binary);
    char ch;
    while (ist.get(ch))
    {
        ost.write((char*)&ch, sizeof(char));
    }
}

int main()
{
    cout << "Enter input file name:\n";
    string ifile;
    cin >> ifile;
    to_binary(ifile);


}

This seems like it should be working to me but it doesn't? I give input a path to some file on my desktop and then call the function but it's just writing the normal text? The file I'm giving as input contains this:

test file idk what to put here but yeah

Then I run this and binaryfile.dat gets the exact same text just normal text binaryfile.dat

Does anyone know what I'm doing wrong? I open ost in binary mode, then get the address of each character I extract from ist, get 1 byte from it and write it to the binary file what's making this output normal text to it..? Edit : Tried this with an int and it worked I got what I expected: This is what I expected A file that obviously a human can't read why does it work with ints and not chars? This is the code I used:

int main()
{
    int a = 5;
    ofstream out("ItemData.dat", ios::binary);
    out.write((char*)&a, sizeof(int));


}

CodePudding user response:

What exactly were you expecting to happen?

There is no such thing as a binary file. It's just a file.

A file is a series of bytes. Nothing more, nothing less.

A text file is a file where each byte "means" a character. For example, the byte value 01000001 means the capital letter A. When you open a file in Notepad, Notepad reads the bytes and displays the corresponding letters. If it sees the byte value 01000001 it displays the capital letter A.

Notepad has no idea whether the file "is a text file" or not. It just looks at the bytes and displays the letters. You can open any file in Notepad, such as an EXE file or a JPEG file, and whenever it happens to contain the byte value 01000001 it will display the capital letter A.

Your code reads bytes from a file in text mode and writes them in binary mode. So it makes a copy of the same file... except for the difference between text mode and binary mode.

So what is that difference? Well, the only difference is that text mode tries to "normalize" line endings. Windows has a tradition that the end of a line of text consists of bytes 00001101 and 00001010 in that order. C has a tradition that the end of a line of text is just the byte 00001010. Text mode does that conversion, so that you can read Windows text files in C.

If the file has the byte value 00001010 without a 00001101 before it, most text editors still display it as a line ending, but until Windows 10, Notepad didn't display it as a line ending. Now Notepad does that too. So you won't see the difference in a text editor. You can see the difference in a hex editor program, which directly shows you the bytes in a file (in hexadecimal). I recommend HxD if you are using Windows.

The main reason for opening binary files in binary mode is because if you open a binary file in text mode, the operating system will add or delete 00001101 bytes which will mess up your binary data. Opening the file in binary mode tells it to please not mess up your binary data.

  •  Tags:  
  • c
  • Related