C++ “ofstream”和“ofstream”有什么区别<<&引用;写

C++ “ofstream”和“ofstream”有什么区别<<&引用;写,c++,visual-c++,file,C++,Visual C++,File,我以二进制模式打开了一个文件,并且喜欢写入一个文件 ofstream ofile("file.txt",ios_base::binary) int a = 1; float f = 0.1; string str = 10; ofile<<a<<f<<str; 文件流的(“file.txt”,ios_base::binary) INTA=1; 浮点数f=0.1; 字符串str=10; ofile运算符AFAIK writ

我以二进制模式打开了一个文件,并且喜欢写入一个文件

   ofstream ofile("file.txt",ios_base::binary)
    int a = 1;
    float f = 0.1;
    string str = 10;
    ofile<<a<<f<<str;
文件流的
(“file.txt”,ios_base::binary)
INTA=1;
浮点数f=0.1;
字符串str=10;

ofile运算符AFAIK write传递值'as is',其中as运算符这是一个完全的猜测,但我打赌
运算符或怀疑它应该
写入(&mystring,mystring.size())
@Dilawar:不,这是错误的。字符数据不存储在字符串对象本身中。它是单独动态分配的,并由指针引用。您可以使用
mystring.c_str()
,但如果您希望能够正确检索它,您还需要以某种方式存储大小本身。
wrtie(&mystring,mystring.length())
更好吗?另外
写入(“s\n”,2)
是否正常?如果是,那么它比文件@platinoob的速度快吗?否,
length()
size()
做完全相同的事情
write(“s\n”,2)
很好,它可能比文件的
更快
write( &mystring, sizeof(std::string) );