Archived

This topic is now archived and is closed to further replies.

The C modest god

Writing and reading string class

Recommended Posts

Fruny    1658
It''s working perfectly fine for me.


$ cat foo.cc
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string a="test", b;
std::ofstream outf("a.fl", std::ios::binary);
std::ifstream inf("a.fl", std::ios::binary);

outf<<a;
outf.close();
inf>>b;
inf.close();

std::cout << b << std::endl;
}


And don''t use [code‍] blocks if you''re not willing to replace your < with &lt;

Share this post


Link to post
Share on other sites
I have noticed that when I read the string, I get the string and the string I wrote afterwards. I assume that writting a string in binary does not put ''\0''.
Therefore I need to put it explicitly.
My question is, does endl functions as ''\0''?

Share this post


Link to post
Share on other sites
Fruny    1658
No. endl works as ''\n'', and >> reads whitespace-delimited words. Opening a file in ''binary mode'' doesn''t mean the data is read/written in binary, only that (platform-dependent) newline translation is not done.

Share this post


Link to post
Share on other sites