LClearner 2017-06-03 11:28 采纳率: 50%
浏览 1009

c++ 输入输出流问题 流

为什么结果和我想的不一样,最后文件中海多了很多乱码

#include
#include
using namespace std;

int main()
{
ofstream worker;
worker.open("g:\worker.dat",ios::out|ios::binary);
int i = 0;
cout<<'?';
cin>>i;
while(i > 0)
{
char name[30],telephone[30],mail[30],stdress[30],czTemp[20];
int code;
if(!worker)
{
cout<<"error"; abort();
}
cin>>name;
worker.write((char*)&name,strlen(name));
cout<<"code"; cin>>code;
itoa(code,czTemp,10);
worker.write(czTemp,strlen(czTemp));
cout<<"telephone"; cin>>telephone;
worker.write((char*)&telephone,strlen(telephone));
cout<<"mail"; cin>>mail;
cout<<"stdress"; cin>>stdress;
worker.write((char*)&stdress,strlen(stdress));
cout.flush();
cout<<'?'<<endl;
i --;
}

worker.close();

ifstream re("g:\\worker.dat",ios::in|ios::binary);
char c[1];


*c=2;i=3;
while(i>0)
{char name[30],telephone[30],mail[30],stdress[30],czTemp[20];
    re.read((char*)&name,strlen(name));
    re.read(czTemp,sizeof(czTemp));
    re.read((char*)&telephone,sizeof(telephone));
    re.read((char*)&name,strlen(name));

    if(strcmp(czTemp,c)==1)
    {cout<<name<<czTemp; break;}//这里也没法跳出循环
    i--;

}


re.close();
system("pause");
return 0;
getchar();
return 0;

}

  • 写回答

1条回答

  • SuperKilling 2017-06-04 03:50
    关注

    it should be worker.open("g:\worker.dat",ios::out|ios::binary);
    missing '\' here, you'd better check file open is successfully or not.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿