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个回答

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

LZCitlearner
LClearner no!!!!!!!!
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问