
c++中string类字符串怎样才能正确地以二进制类型输入文件啊
我写了个程序,是以二进制写入,但打开文件却发现不是乱码#include<string>#include<fstream>usingnamespacestd;intmain...
我写了个程序,是以二进制写入,但打开文件却发现不是乱码
#include<string>
#include<fstream>
using namespace std;
int main(){
string str;
getline(cin,str);//输入
ofstream fout("a.dat",ios::binary);
fout.write(str.c_str(),str.size());
fout.close();
return 0;
} 展开
#include<string>
#include<fstream>
using namespace std;
int main(){
string str;
getline(cin,str);//输入
ofstream fout("a.dat",ios::binary);
fout.write(str.c_str(),str.size());
fout.close();
return 0;
} 展开
1个回答
展开全部
你对文本文件的认识还是不清楚,文本文件本质上还是二进制文件,只是它的内容都是些ASCII码(或者是汉字等双字节字符),你输入的都是ASCII码,那打开看自然不会是乱码
要想看乱码,就弄个int数组,然后用write写入文件就看出效果了
要想看乱码,就弄个int数组,然后用write写入文件就看出效果了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询