返回信息流代码如下:
#include<iostream>
#include<fstream>
#include<string.h>
#include<time.h>
using namespace std;
int main()
{
char name[128];
char password[128];
char timebuf[256];
char body[50];
cout << "请输入用户名" << endl;
cin >> name;
cout << "请输入密码" << endl;
cin >> password;
cout << "登录成功,登录记录已写入日志" << endl;
fstream file;
file.open("test.dat",ios::binary|ios::out);
time_t tCurrentTime;
tCurrentTime = time(NULL);
strftime ( timebuf, sizeof ( timebuf ), "%H:%M:%S %Y-%m-%d ", localtime ( &tCurrentTime ) );
file.write(timebuf,256);
file.write(name,128);
strcpy(body,"登录.");
file.write(body,50);
file.close();
return 0;
}
执行后,发现test.dat里面系统时间正常显示,但是后面是一堆乱码(乱码中有输入的用户名和登录两个字)
哪里有问题,如何修改?
这是一条镜像帖。来源:北邮人论坛 / cpp / #94050同步于 2016/11/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【问题】为什么会出现乱码?
flgkd
2016/11/25镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
不是。binary只解决换行符自动转换的问题,并不能解决字符编码问题。
【 在 xiaobing307 的大作中提到: 】
: ls应当是正解