LZCitlearner
LClearner
采纳率65.5%
2017-06-03 11:28 阅读 1.0k

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 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.

    点赞 评论 复制链接分享

相关推荐