Reputation: 13876
I'm very unsure of file IO when it comes to this, and I'm really confused. When I use the insertion stream operator to push some short integers into the file the size of the file is not what I expect:
std::ofstream outFile("myFile.raw", std::ios::binary | std::ios::out);
std::vector<short> list;
for (int i = 0; i < 100; ++i) list.push_back(1);
for (auto i : list) outFile << i;
outFile.close();
This creates a file that is 100 bytes big. I'm not understanding, the short is supposed to be two bytes. The documentation shows that the << operator is overloaded for many different types, so I thought that this writes the correct data type. I guess not.
Thanks.
Upvotes: 0
Views: 1486
Reputation: 35154
The reason is - as mentioned in the comments an in the previous answer - that operator <<
is formatted output and writes value 1
as a single digit (which consists of only one character).
Just for the sake of showing how a short int could be written as binary, see the following code:
std::ofstream outFile("myFile.raw", std::ios::binary | std::ios::out);
std::vector<short> list;
for (int i = 0; i < 100; ++i) list.push_back(1);
for (auto i : list) outFile.write((const char*)&i, sizeof(i));
outFile.close();
std::ifstream in("myFile.raw", std::ifstream::ate | std::ifstream::binary);
std::cout << "File size: " << in.tellg() << std::endl;
// prints out: File size: 200
in.close();
Upvotes: 1
Reputation: 11002
You are using formatted output.
The short int
s are written as digits. Each digit is 1
which is a single char
of a single byte.
So the total is 100. If you had short int
s containing double digit numbers, say 22
, then the size would be 200.
As an alternative you could either use unformatted output or you could cast the data to char
s, which together with the binary
flag you have set will write the data without formatting it. Something to keep in mind when writing raw char
s is that you will have to consider how many bytes and how you want to order the bytes in the file.
Formatted output and input could be more convenient in general. As an alternative to writing the file with raw char
s it would be better to look into serialization.
Upvotes: 4