0

I used a string (mystring) to put on it the content of a file :

std::ofstream fs("C:\\example.mp4",std::ios::binary);
fs << mystring;
fs.close();

this works with several examples and I wonder why ? (even with files that contains zeros like a png file for example), I mean the binary file is correctly saved on the HDD...

Otherwise, I used the size() method to determine the size of the data that the string contains in order to upload it,... I know that in some binary files there are zeros ('\O'), I read that this method is similar to length() so it stops counting if it finds a '\0' ? but even in that case my application works, I mean, size() gave me the true size of my data (binary file), for this case I want to precise that I use this type of construction :

//...
std::ifstream::pos_type fileSize = ifs.tellg();
std::vector<char> bytes(fileSize);
//...
myifstream.read(&bytes[0], fileSize);
file_content = std::string(&bytes[0], fileSize);

so size() will return the value of myifstream.tellg() ? and not counting until it finds a '\0' ??

in another situation, the server sends me data (char*), I used append(data,size) to copy them in the string... and it always works even if there are zeros....

I want to ensure that using a string object to manipulate a file content is safe. (file stream and size() )

Aminos
  • 754
  • 1
  • 20
  • 40
  • 1
    possible duplicate of [Can a std::string contain embedded nulls?](http://stackoverflow.com/questions/2845769/can-a-stdstring-contain-embedded-nulls) – Diego Jan 18 '15 at 00:36
  • You probably mean `fs >> mystring;` ? Or are you dumping the `string` in the file? Not clear from your question. – vsoftco Jan 18 '15 at 00:50
  • Diego Thank you, @vsoftco the string already contains the data I received from a HTTP server, and I want to put that data in a file, the data can be anything, a PNG file for example that surely contains zeros, anyway my code works, and the purpose of my question, I want to know how things work, why fs << mystring works and how size() returns the quantity of bytes (including zeros \0) – Aminos Jan 18 '15 at 12:27

2 Answers2

2

You are probably better off using std::vector<uint8_t> for a byte stream instead of strings, not because strings don't work, but because semantically, strings are meant for text strings, i.e. something that you might push to cout.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • Exactly - using a vector of u8 for binary data is more appropriate to avoid questions of null terminators, etc, that come up when using std::string. – tmruss Jan 18 '15 at 02:22
1

To answer your first question, the underlying data type for a string is just an array of char, so reading in data which doesn't have printable characters will still work, but if you tried to print it it would (most likely) be gibberish. Reading and reprinting a file one byte at a time is not otherwise undefined behavior as far as I know, but someone can correct me if I'm wrong.

As for dealing well with nulls, the linked question answers that, but basically string keeps track of its size anyway, so there was no reason to force them to end with null characters.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17