流“"和“"之间的区别是什么?和写 [英] Whats the difference between ofstream "<<" and Write

查看:68
本文介绍了流“"和“"之间的区别是什么?和写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以二进制模式打开了一个文件,并且想写一个文件.

I had opened a file in binary mode and like to write to an file .

   ofstream ofile("file.txt",ios_base::binary)
    int a = 1;
    float f = 0.1;
    string str = 10;
    ofile<<a<<f<<str;

喜欢知道使用<<"书写之间的区别并使用"ofile.write". 这是用二进制模式编写的最佳方法.

Like to know what the difference between writing using "<<" and using "ofile.write" . which is the best and effective to write in binary mode.

推荐答案

operator<<会将您的数据格式化为文本.而write将以与ram中存储的格式相同的格式输出数据.因此,如果要编写二进制文件,则要使用write.

operator<< will format your data as text. Whereas write will output data in the same format as it's stored in ram. So, if you are writing binary files, you want to use write.

但是,如果要编写非Pod类型,则需要小心.你不能只说:

However, if you are writing non-pod types, you need to be careful. You can't just say:

write( &mystring, sizeof(std::string) );

您需要某种方法来输出实际数据,该数据不会存储在类或结构本身中.

You need to have some way to output the actual data, which is not stored in the class or struct itself.

这篇关于流“"和“"之间的区别是什么?和写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆