主题:请各位大神运行一下,为什么我的代码文件输入输出不成功啊
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
struct book
{char name[20];
char no[10];
int num;
float price;
};
void main()
{
fstream iolib("data.txt",ios::out|ios::in,ios::binary);
if(!iolib)
{cout<<"flie can't open"<<endl;
exit(1);}
book temp;
for(int i=0;i<20;i++)
{cin>>temp.name >>temp.no>>temp.num>>temp.price;
iolib.write((char*)&temp,sizeof(book));}
for(int i=20;i>10;i--)
{
iolib.seekg((i-1)*sizeof(book),ios::beg);
iolib.read((char*)&temp,sizeof(book));
iolib.write((char*)&temp,sizeof(book));
}
cin>>temp.name >>temp.no>>temp.num>>temp.price;
iolib.seekg(10*sizeof(book),ios::beg);
iolib.write((char*)&temp,sizeof(book));
int total=0;
float sum=0.0;
iolib.seekg(0,ios::beg);
{cout<<"name"<<'\t'<<"no"<<'\t'<<"num"<<'\t'<<"price"<<endl;}
for(int i=0;i<20;i++)
{iolib.read((char*)&temp,sizeof(book));
cout<<temp.name<<temp.no<<temp.num<<temp.price<<endl;
total+=temp.num;
sum+=temp.num*temp.price;
}
cout<<"total number:"<<total<<'\t'<<"amount"<<sum;
}
#include<fstream>
#include<stdlib.h>
using namespace std;
struct book
{char name[20];
char no[10];
int num;
float price;
};
void main()
{
fstream iolib("data.txt",ios::out|ios::in,ios::binary);
if(!iolib)
{cout<<"flie can't open"<<endl;
exit(1);}
book temp;
for(int i=0;i<20;i++)
{cin>>temp.name >>temp.no>>temp.num>>temp.price;
iolib.write((char*)&temp,sizeof(book));}
for(int i=20;i>10;i--)
{
iolib.seekg((i-1)*sizeof(book),ios::beg);
iolib.read((char*)&temp,sizeof(book));
iolib.write((char*)&temp,sizeof(book));
}
cin>>temp.name >>temp.no>>temp.num>>temp.price;
iolib.seekg(10*sizeof(book),ios::beg);
iolib.write((char*)&temp,sizeof(book));
int total=0;
float sum=0.0;
iolib.seekg(0,ios::beg);
{cout<<"name"<<'\t'<<"no"<<'\t'<<"num"<<'\t'<<"price"<<endl;}
for(int i=0;i<20;i++)
{iolib.read((char*)&temp,sizeof(book));
cout<<temp.name<<temp.no<<temp.num<<temp.price<<endl;
total+=temp.num;
sum+=temp.num*temp.price;
}
cout<<"total number:"<<total<<'\t'<<"amount"<<sum;
}