Skip to main content
GameDev.net gamedev.net
🔒 Locked

Writing and reading string class

Started by The C modest god Jan 4, 2004 at 3:07 AM 2 replies 250+ views
Fruny
Fruny
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;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
The C modest god
The C modest god
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''?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.