
帮忙修改一道c语言文件操作题
问题:当我输入4时可以往文件中输入新增的城市信息但是再输入2时只能将原来输入的数输出无法输出新增城市的信息各位帮忙看看啊结果满意有赠分#include<iostream....
问题:
当我输入4时 可以往文件中输入新增的城市信息
但是再输入2时 只能将原来输入的数输出
无法输出新增城市的信息
各位帮忙看看啊
结果满意有赠分
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
struct city{
char cityname[20];
char country[50];
char state[50];
int population;
char underground;
char mayor_name[50];
}city[2];
void menu(){
cout<<"^_^ Choose number : "<<endl;
cout<<"1.Input;"<<endl
<<"2.Output;"<<endl
<<"3.Research;"<<endl
<<"4.Rewrite;"<<endl
<<"5.Quit;"<<endl;
}
void input(){
for(int i=0; i<2; i++){
cout<<"Enter the name of city:";cin>>city[i].cityname;
cout<<"Enter the name of the country:";cin>>city[i].country;
cout<<"Enter the name of state:";cin>>city[i].state;
cout<<"Enter the number of the population:";cin>>city[i].population;
cout<<"Enter the underground(y/n):"; cin>>city[i].underground;
cout<<"Enter the name of mayor:";cin>>city[i].mayor_name;
cout<<endl;
}
}
void output(struct city* m){
ifstream fin("d:\\struct_city.txt",ios::binary);
fin.open("d:\\struct_city.txt",ios::binary);
fin.read((char*)&city,sizeof(city));
fin.close();
for(int k=0; k<2; k++){
cout<<"The name of city:" <<m->cityname<<endl
<<"The name of the country:"<<m->country<<endl
<<"The name of state:"<<m->state<<endl
<<"The number of the population:"<<m->population<<endl
<<"The underground(y/n):"<<m->underground<<endl
<<"The name of mayor:"<<m->mayor_name<<endl;
cout<<endl;
m++;
}
}
int function(struct city & f_city);
int main()
{
FILE* fp;
fp=fopen("d:\\struct_city.txt","wb+");
struct city* pt;
pt=city;
menu();
char i;
while(cin>>i){
switch(i){
case '1' : {
input();
fwrite(pt,sizeof(struct city),2,fp);
menu();
continue; }
case '2' : {
rewind(fp);
fread(pt,sizeof(struct city),2,fp);
output(pt);
menu();
continue;}
写不下了 接下面的地址
http://zhidao.baidu.com/question/43817734.html
麻烦了
结果满意百分
题目要求:
文件操作 我用的是switch
当case1时 向文件中输入两座城市信息
当case2时 输出信息(我的问题在这里)
当case3时 可以查询信息
当case4时 添加一座新城市信息
当case5时 退出 展开
当我输入4时 可以往文件中输入新增的城市信息
但是再输入2时 只能将原来输入的数输出
无法输出新增城市的信息
各位帮忙看看啊
结果满意有赠分
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
struct city{
char cityname[20];
char country[50];
char state[50];
int population;
char underground;
char mayor_name[50];
}city[2];
void menu(){
cout<<"^_^ Choose number : "<<endl;
cout<<"1.Input;"<<endl
<<"2.Output;"<<endl
<<"3.Research;"<<endl
<<"4.Rewrite;"<<endl
<<"5.Quit;"<<endl;
}
void input(){
for(int i=0; i<2; i++){
cout<<"Enter the name of city:";cin>>city[i].cityname;
cout<<"Enter the name of the country:";cin>>city[i].country;
cout<<"Enter the name of state:";cin>>city[i].state;
cout<<"Enter the number of the population:";cin>>city[i].population;
cout<<"Enter the underground(y/n):"; cin>>city[i].underground;
cout<<"Enter the name of mayor:";cin>>city[i].mayor_name;
cout<<endl;
}
}
void output(struct city* m){
ifstream fin("d:\\struct_city.txt",ios::binary);
fin.open("d:\\struct_city.txt",ios::binary);
fin.read((char*)&city,sizeof(city));
fin.close();
for(int k=0; k<2; k++){
cout<<"The name of city:" <<m->cityname<<endl
<<"The name of the country:"<<m->country<<endl
<<"The name of state:"<<m->state<<endl
<<"The number of the population:"<<m->population<<endl
<<"The underground(y/n):"<<m->underground<<endl
<<"The name of mayor:"<<m->mayor_name<<endl;
cout<<endl;
m++;
}
}
int function(struct city & f_city);
int main()
{
FILE* fp;
fp=fopen("d:\\struct_city.txt","wb+");
struct city* pt;
pt=city;
menu();
char i;
while(cin>>i){
switch(i){
case '1' : {
input();
fwrite(pt,sizeof(struct city),2,fp);
menu();
continue; }
case '2' : {
rewind(fp);
fread(pt,sizeof(struct city),2,fp);
output(pt);
menu();
continue;}
写不下了 接下面的地址
http://zhidao.baidu.com/question/43817734.html
麻烦了
结果满意百分
题目要求:
文件操作 我用的是switch
当case1时 向文件中输入两座城市信息
当case2时 输出信息(我的问题在这里)
当case3时 可以查询信息
当case4时 添加一座新城市信息
当case5时 退出 展开
2个回答
展开全部
晕阿
1 struct city{...}city[2];
case 4 里 sizeof(city) 错了 改为sizeof(struct city) 造成乱码。
2 另外 function怎么没返回值阿
3 fread(pt,sizeof(struct city),2,fp); 只读前两个
4 fp=fopen("d:\\struct_city.txt","wb+"); 每次都覆盖原来数据
1 struct city{...}city[2];
case 4 里 sizeof(city) 错了 改为sizeof(struct city) 造成乱码。
2 另外 function怎么没返回值阿
3 fread(pt,sizeof(struct city),2,fp); 只读前两个
4 fp=fopen("d:\\struct_city.txt","wb+"); 每次都覆盖原来数据
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询