C++生成的文件为什么信息没写入? 代码: #include<iostream.h> #include<fstream.h> #include<stdlib.h>
代码:#include<iostream.h>#include<fstream.h>#include<stdlib.h>structPerson{charname[10]...
代码:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct Person
{
char name[10];
double height;
unsigned age;
};
Person people[4]={{"wang",1.65,25},{"zhang",1.72,24},{"li",1.89,21},{"huang",1.70,22}};
void main()
{
fstream outfile;
fstream infile;
outfile.open("d:\\f1.dat",ios::in|ios::binary);
if(!outfile)
{ cout<<"No such file!"<<endl;
abort();
}
for(int i=0;i<4;i++)
outfile.write((char*)&people[i],sizeof(people[i]));
outfile.close();
infile.open("f1.dat",ios::in|ios::binary);
if(!infile)
{
cout<<"No such file!"<<endl;
abort();
}
for(int j=0;j<4;j++)
{
infile.read((char*)&people[j],sizeof(people[j]));
cout<<people[j].name<<"\t"<<people[j].height<<"\t"<<people[j].age<<endl;
}
infile.close();
} 展开
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct Person
{
char name[10];
double height;
unsigned age;
};
Person people[4]={{"wang",1.65,25},{"zhang",1.72,24},{"li",1.89,21},{"huang",1.70,22}};
void main()
{
fstream outfile;
fstream infile;
outfile.open("d:\\f1.dat",ios::in|ios::binary);
if(!outfile)
{ cout<<"No such file!"<<endl;
abort();
}
for(int i=0;i<4;i++)
outfile.write((char*)&people[i],sizeof(people[i]));
outfile.close();
infile.open("f1.dat",ios::in|ios::binary);
if(!infile)
{
cout<<"No such file!"<<endl;
abort();
}
for(int j=0;j<4;j++)
{
infile.read((char*)&people[j],sizeof(people[j]));
cout<<people[j].name<<"\t"<<people[j].height<<"\t"<<people[j].age<<endl;
}
infile.close();
} 展开
1个回答
展开全部
//具体错误在程序中,顺便帮你改进了程序
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct Person
{
char name[10];
double height;
unsigned age;
};
Person people[4]={{"wang",1.65,25},{"zhang",1.72,24},{"li",1.89,21},{"huang",1.70,22}};
Person peopl[4];//新建一个person用来测试infile是否成功
void main()
{
fstream outfile;
fstream infile;
outfile.open("d:\\f1.dat",ios::out|ios::binary); //ios::out不是ios::in
if(!outfile)
{ cout<<"No such file!"<<endl;
abort();
}
for(int i=0;i<4;i++)
outfile.write((char*)&people[i],sizeof(people[i]));
outfile.close();
infile.open("d:\\f1.dat",ios::in|ios::binary);//文件名地址要写全
if(!infile)
{
cout<<"No such file!"<<endl;
abort();
}
for(int j=0;j<4;j++)
{
infile.read((char*)&peopl[j],sizeof(peopl[j]));//用测试数组接受数据
cout<<peopl[j].name<<"\t"<<peopl[j].height<<"\t"<<peopl[j].age<<endl;
}
infile.close();
}
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct Person
{
char name[10];
double height;
unsigned age;
};
Person people[4]={{"wang",1.65,25},{"zhang",1.72,24},{"li",1.89,21},{"huang",1.70,22}};
Person peopl[4];//新建一个person用来测试infile是否成功
void main()
{
fstream outfile;
fstream infile;
outfile.open("d:\\f1.dat",ios::out|ios::binary); //ios::out不是ios::in
if(!outfile)
{ cout<<"No such file!"<<endl;
abort();
}
for(int i=0;i<4;i++)
outfile.write((char*)&people[i],sizeof(people[i]));
outfile.close();
infile.open("d:\\f1.dat",ios::in|ios::binary);//文件名地址要写全
if(!infile)
{
cout<<"No such file!"<<endl;
abort();
}
for(int j=0;j<4;j++)
{
infile.read((char*)&peopl[j],sizeof(peopl[j]));//用测试数组接受数据
cout<<peopl[j].name<<"\t"<<peopl[j].height<<"\t"<<peopl[j].age<<endl;
}
infile.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询