tags:

views:

152

answers:

1

Edit: I'm trying to convert a text file into bytes. I'm not sure if the code is turning it into bytes or not. Here is the link to the header so you can see the as_bytes function.

link

#include "std_lib_facilities.h"

int main()
{
    cout << "Enter input file name.\n";
    string file;
    cin >> file;
    ifstream in(file.c_str(), ios::binary);

    int i;
    vector<int> bin;

    while(in.read(as_bytes(i), sizeof(int)))
        bin.push_back(i);

    ofstream out(file.c_str(), ios::out);

    for(int i = 0; i < bin.size(); ++i)
            out << bin[i];

    keep_window_open();
}

Note that now the out stream just outputs the contents of the vector. It doesn't use the write function or the binary mode. This converts the file to a large line of numbers - is this what I'm looking for?

Here is an example of the second code's file conversion:

that guy likes to eat lots of pie (not sure if this was exact text)

turns to

543518319544825700191924850016351970295432362115448292821701667182186922608417526375411952522351186935715718643976841768956006
+2  A: 

The reason your first method didn't change the file is because all files are stored in the same way. The only "difference" between text files and binary files is that text files contain only bytes that can be shown as ASCII characters, while binary files* have a much more random variety and order of bytes. So you are reading bytes in as bytes and then outputting them as bytes again!

*I'm including Unicode text files as binary, since they can have multiple bytes to denote one character point, depending on the character point and the encoding used.

The second method is also fairly simple. You are reading in the bytes, as before, and storing them in integers (which are probably 4 bytes long). Then you are just printing out the integers as if they are integers, so you are seeing a string of numbers.

As for why your first method cut off some of the bytes, you're right in that it's probably some bug in your code. I thought it was more important to explain what the ideas are in this case, rather than debug some test code.

Sean Nyman
Yes, that's what I mean to do was to read them as bytes rather than the actual words. However, the book still refers to them as binary files.
trikker
all files are binary files, the opening mode just determines how to read and treat the contents e.g. when you open a file in text mode and a '\n' value is found it is interpreted as a newline, when opened in binary mode it is not interpreted.
Anders K.
So what I'm trying to do is keep them in their byte form and output them as such. And eventually turns the bytes back into the words that they were. I just have no idea if I'm on the right track or not.
trikker
No, you're misunderstanding the concepts. There is no such thing as "the words that they were". All files are stored on the computer as digital, "binary", files. All text is just a series of bytes that the computer can show as text when it's asked to (like in a text editor or command prompt). You can't translate between the "byte form" and the "word form" because there is no alternative to the "byte form".
Sean Nyman
So then how are image files and music files transferred if they can't be converted back and forth?
trikker
Bytes are made up of bits, which are binary numbers to represent data. I believe the most number of bits in a byte is 8. Therefore, a byte can represent a character, a number, etc. I don't know how images/audio files are represented, but that is very complex. In this case, one byte represents one char. The computer sees bytes - the operating system converts that to characters at runtime.
Hooked