Binary files

I am trying to write some info to a binary file. When I open the file with Notepad, I expect to see some nonsense, but I see normal characters. I can't identify the mistake...
Program asks the user to input a string and a number, then to write it into binary file. Here is the chunk of code.

1
2
3
4
5
6
7
8
9
10
11
    char name[64];
    int num;
    cout << "Input name      ";
    cin.getline(name, 64);
    cout << "\nInput number    ";
    cin >> num; cin.ignore(10000, '\n'); cin.clear();
    ofstream input("file", ios::out | ios::app | ios::binary); //make it a binary file
    input << num << "    ";
    for(int i(0); name[i] != 0; i++)
        input << name[i];
    input << '\n';
Try outputting some numeric values of type int or double and see how it looks.
I dropped everything in the input except variable of type int. No change, I see in the file what I input in program.
Use unformatted i/o

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

int main()
{
    int numbers[] = { 62, 70, 98, 71, 81, 99, 74, 80, 73, 88, 73, 72, 95, 71 } ;
    std::ofstream file( "numbers.dat", std::ios::app|std::ios::binary ) ;
    file.write( reinterpret_cast< const char* >( +numbers ), sizeof(numbers) ) ;
}
Disch wrote:
val = bytes[0] | (bytes[1] << 8); // construct the 16-bit value from those bytes


If the returned value from bytes[1] isn't cast into u16, won't the shift operation result in losing all the bit information?
I found solution. Methods read and write are made for binary files. Overloaded operators << and >> are for inputting in normal text files.

Thank you, anyway. :)
Topic archived. No new replies allowed.