C++中fstream流修改二进制文件中指定数据的问题 5
请求帮助检查源代码#include<iostream>#include<string>#include<fstream>usingnamespacestd;structF...
请求帮助检查源代码
#include <iostream>#include <string>
#include <fstream>
using namespace std;
struct FooD
{
int sequence;
char name[100];
int number;
double price;
};
void main()
{
FooD seeds;
seeds.sequence=1;
strcpy(seeds.name,"seeds");
seeds.number=20;
seeds.price=2.5;
FooD cake; cake.sequence=2;
strcpy(cake.name,"cake");
cake.number=8;
cake.price=6.0;
FooD biscuit; biscuit.sequence=3;
strcpy(biscuit.name,"biscuit");
biscuit.number=15;
biscuit.price=4.5;
FooD milk; milk.sequence=4;
strcpy(milk.name,"milk");
milk.number=30;
milk.price=1.8;
/////////////write into file/////////////////////////
ofstream write;
write.open("Update.dat",ios::binary);
if(!write)
cout<<"Creat file error!"<<endl;
else
{
write.write((char *)&seeds,sizeof(FooD));
write.write((char *)&cake,sizeof(FooD));
write.write((char *)&biscuit,sizeof(FooD));
write.write((char *)&milk,sizeof(FooD));
write.close();
}
///////////////////////////////////////////////////////
////////////////read from the file//////////////////// ifstream read;
FooD one;
read.open("Update.dat",ios::ate|ios::binary);
if(!read)
cout<<"open file error."<<endl;
else
{
read.seekg(0,ios::beg);//locate the pointer of file to the beginning.
while(read.peek()!=EOF)
{
read.read((char *)&one,sizeof(FooD));
cout<<one.name<<'\t'<<one.number<<'\t'<<one.price<<endl;
}
cout<<"END!"<<endl;
read.close();
}
//////////////////////////////////////////////////////
///////////////update the file//////////////////////// FooD updatefood;
updatefood.sequence=2;
strcpy(updatefood.name,"cake");
updatefood.number=8;
updatefood.price=6.0;
updatefood.number=6;
fstream update; update.open("Update.dat",ios::in|ios::out|ios::binary);
if(!update)
cout<<"open file error."<<endl;
else
{
while(update.peek()!=EOF)//问题发生在这个while循环中
{
update.read((char *)&one,sizeof(FooD));
if(strcmp(one.name,cake.name)==0)
{
update.seekp(-sizeof(FooD),ios::cur);
update.write((char *)&updatefood,sizeof(FooD));
}
}
update.close();
}
//////////////////////////////////////////////////////
/////////////////read the file//////////////////////// read.open("Update.dat",ios::in);
if(!read)
cout<<"open file error."<<endl;
else
{
while(read.peek()!=EOF)
{
read.read((char *)&one,sizeof(FooD));
cout<<one.name<<'\t'<<one.number<<'\t'<<one.price<<endl;
}
read.close();
}
//////////////////////////////////////////////////////
return;} 展开
#include <iostream>#include <string>
#include <fstream>
using namespace std;
struct FooD
{
int sequence;
char name[100];
int number;
double price;
};
void main()
{
FooD seeds;
seeds.sequence=1;
strcpy(seeds.name,"seeds");
seeds.number=20;
seeds.price=2.5;
FooD cake; cake.sequence=2;
strcpy(cake.name,"cake");
cake.number=8;
cake.price=6.0;
FooD biscuit; biscuit.sequence=3;
strcpy(biscuit.name,"biscuit");
biscuit.number=15;
biscuit.price=4.5;
FooD milk; milk.sequence=4;
strcpy(milk.name,"milk");
milk.number=30;
milk.price=1.8;
/////////////write into file/////////////////////////
ofstream write;
write.open("Update.dat",ios::binary);
if(!write)
cout<<"Creat file error!"<<endl;
else
{
write.write((char *)&seeds,sizeof(FooD));
write.write((char *)&cake,sizeof(FooD));
write.write((char *)&biscuit,sizeof(FooD));
write.write((char *)&milk,sizeof(FooD));
write.close();
}
///////////////////////////////////////////////////////
////////////////read from the file//////////////////// ifstream read;
FooD one;
read.open("Update.dat",ios::ate|ios::binary);
if(!read)
cout<<"open file error."<<endl;
else
{
read.seekg(0,ios::beg);//locate the pointer of file to the beginning.
while(read.peek()!=EOF)
{
read.read((char *)&one,sizeof(FooD));
cout<<one.name<<'\t'<<one.number<<'\t'<<one.price<<endl;
}
cout<<"END!"<<endl;
read.close();
}
//////////////////////////////////////////////////////
///////////////update the file//////////////////////// FooD updatefood;
updatefood.sequence=2;
strcpy(updatefood.name,"cake");
updatefood.number=8;
updatefood.price=6.0;
updatefood.number=6;
fstream update; update.open("Update.dat",ios::in|ios::out|ios::binary);
if(!update)
cout<<"open file error."<<endl;
else
{
while(update.peek()!=EOF)//问题发生在这个while循环中
{
update.read((char *)&one,sizeof(FooD));
if(strcmp(one.name,cake.name)==0)
{
update.seekp(-sizeof(FooD),ios::cur);
update.write((char *)&updatefood,sizeof(FooD));
}
}
update.close();
}
//////////////////////////////////////////////////////
/////////////////read the file//////////////////////// read.open("Update.dat",ios::in);
if(!read)
cout<<"open file error."<<endl;
else
{
while(read.peek()!=EOF)
{
read.read((char *)&one,sizeof(FooD));
cout<<one.name<<'\t'<<one.number<<'\t'<<one.price<<endl;
}
read.close();
}
//////////////////////////////////////////////////////
return;} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询