-1
#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));


}
tommyd
  • 21
  • 4
  • 3
    Yes, you're reading each `char` from one file and then you write the same `char`s to another file one by one. What did you expect to find, if not exactly the same data? I think you have misunderstood "binary". – molbdnilo Nov 17 '21 at 12:38
  • Text is a special subset of binary. What exactly were you expecting? Also, it's good to always close your streams after use. – 500 - Internal Server Error Nov 17 '21 at 12:39
  • I remember doing something similar and the text file contained random characters that can't be read by a human that's what I assumed a binary file was and so why isn't that happening here? Am I not writing bytes to a file here? – tommyd Nov 17 '21 at 12:44
  • For example in this video: https://www.youtube.com/watch?v=soRm-VQmdLA&t=383s this guy seems to do something similar to what I did and his text file contains these random symbols that a human can't understand that's what I want why is this not happening here? – tommyd Nov 17 '21 at 12:46
  • It's happening here because you don't change the value of `ch` – drescherjm Nov 17 '21 at 12:46
  • 2
    @500-InternalServerError in this case it is not. The scope is very limited (which is good), so the RAII will handle the closing. If you *have* to manually close the stream, that means that your function is probably too long... – Sergey Kolesnik Nov 17 '21 at 12:47
  • Don't change the value of ch? Well I mean I'm just getting a char from a file and sure its gets assigned to ch but why does that matter? I opened an ofstream in binary mode and I'm using the write method giving it the address of the char and telling it to just get one byte and write that to my .dat file I don't understand why it's writing normal text – tommyd Nov 17 '21 at 12:51
  • 1
    What exactly were you **expecting** to happen? There is no such thing as a binary file, it's just a file. Text mode means the operating system can adjust line endings in a way that makes sense for text, that's all. – user253751 Nov 17 '21 at 12:54
  • ***Well I mean I'm just getting a char from a file and sure its gets assigned to ch but why does that matter?*** It matters because the char will be the same value you read and you seemed to want it to instead be a different value having some unreadable symbols ... – drescherjm Nov 17 '21 at 12:56
  • I already said what I expected to happen, why am I getting normal characters in the file shouldn't writing in binary mode write the bytes to the file and when I open it the file will contain characters that obviously aren't readable by humans? I also sent a video where what I expected to happen happens, he writes to a file in binary mode and then the file contains these weird characters not just normal text. – tommyd Nov 17 '21 at 12:56
  • I don't want it to be a different value I just wanna print the byte to the file. If you look at the video I sent the uploader also writes to a binary file and he gives the address of an object that also contains some data but once it gets written to the file it's not just the value that gets printed it's just a non human readable character which I presume is just the bytes being written to the file – tommyd Nov 17 '21 at 12:58
  • @SergeyKolesnik: Oh, right - I forgot about RAII (I don't do c++ daily ATM). – 500 - Internal Server Error Nov 17 '21 at 12:58
  • Outside of line endings which @user253751 mentioned a char will not change using the method you are using. If you had a file of integers that were text and used int to store in binary the file would be very different. – drescherjm Nov 17 '21 at 13:03
  • @tommyd It does write the byte to the file. 01000001 is the letter A, and it is not "unreadable by humans", it's still the same character whether you write it in text or binary mode. Actually humans can't see the 01000001 (without a hex editor), what you are seeing is your text editor interpreting 01000001 as a letter A. See my answer. – user253751 Nov 17 '21 at 13:07
  • 1
    The person in the video does not write text to the file, but a data structure he defined himself. The opening mode has nothing to do with it. If the person in the video claims that it has, look for better videos, or - best of all - a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 17 '21 at 13:09
  • Okay so how come notepad can interpret what I wrote if its a char but not an int? – tommyd Nov 17 '21 at 13:15

1 Answers1

2

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.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I did this: int a = 5; ofstream out("ItemData.dat", ios::binary); out.write((char*)&a, sizeof(int)); and itemdata.dat then contained a character that obviously isn't readable by a human: I'll put it in the post but that is what I expected to happen why does that happen with ints but not chars? – tommyd Nov 17 '21 at 13:11
  • A char is the same 1 byte. An integer when read in a text is a series of characters. In binary mode using the method you describe its the 4 or 8 (or other) byte representation that the machine uses to store in memory. – drescherjm Nov 17 '21 at 13:15
  • @tommyd Because the `a` variable contains the bytes 00000101 00000000 00000000 00000000 (depending on your computer/OS but I'm probably right) and 00000101 is not the same as any displayable character (check the ASCII table). But try doing the same thing with a=65 which is 01000001 00000000 00000000 00000000, and you might notice the first byte is displayed as a capital letter A. Then try with a=1145258561 or a=1498238804. – user253751 Nov 17 '21 at 13:21