首页 新闻 会员 周边 捐助

关于文件读写问题,求帮忙看看代码!谢谢!

0
悬赏园豆:20 [已解决问题] 解决于 2015-05-25 00:27
  1 /*文件存储和读取那里不知道哪错了,一直没成功QAQ*/
  2 
  3 
  4 #include<stdio.h>
  5 #include<string.h>
  6 #include<stdlib.h>
  7 #include<conio.h>
  8 #define LEN sizeof(struct Addrbook)
  9 
 10 struct Addrbook{
 11     int num;
 12     char name[20];
 13     char ID[20];
 14     char cla[20];
 15     char major[20];
 16     char tel[20];
 17     char age[10];
 18     char birth[10];
 19     char email[30];
 20     char dom[30];
 21     char home[30];
 22 }addr[100];
 23 
 24 int num=0; 
 25  
 26 void add();
 27 void show();
 28 void modify();
 29 void del();
 30 void menu();
 31 void save();
 32 void read();
 33 
 34 int main()
 35 {
 36     read();
 37     show();
 38     menu();
 39     return 0;    
 40 }
 41 
 42 
 43 void menu()
 44 {
 45     system("cls");
 46     printf("\n\t\t***********************MENU************************");
 47     printf("\n\t\t           1--Add               2--Show            ");
 48     printf("\n\t\t           3--Modify            4--Delete          ");
 49     printf("\n\t\t           5--Save              6--Exit            ");
 50     char n;
 51     printf("\n\t\tEnter the number:");
 52     scanf("%s",&n);
 53     fflush(stdin);
 54     switch(n)
 55     {
 56         case '1':add();break;
 57         case '2':show();break;
 58         case '3':modify();break;
 59         case '4':del();break;
 60         case '5':save();break;
 61         case '6':exit(0);
 62         default:printf("Error!");break;
 63     }
 64 }
 65 
 66 
 67 void add()
 68 {
 69     printf("\n\t\t********Please enter the information*******");
 70     printf("\n\t\tNo.%d",num+1);
 71     printf("\n\t\tName: ");
 72     scanf("%s",&addr[num].name);
 73     printf("\t\tID:   ");
 74     scanf("%s",&addr[num].ID);
 75     printf("\t\tClass ");
 76     scanf("%s",&addr[num].cla);
 77     printf("\t\tMajor:");
 78     scanf("%s",&addr[num].major);
 79     printf("\t\tTel:  ");
 80     scanf("%s",&addr[num].tel);
 81     printf("\t\tAge:  ");
 82     scanf("%s",&addr[num].age);
 83     printf("\t\tBirth:");
 84     scanf("%s",&addr[num].birth);
 85     printf("\t\tEmail:");
 86     scanf("%s",&addr[num].email);
 87     printf("\t\tDom:  ");
 88     scanf("%s",&addr[num].dom);
 89     printf("\t\tHome: ");
 90     scanf("%s",&addr[num].home);
 91     num++;
 92     
 93     printf("\n\t\tEnd adding?(Y/N):");
 94     if(getch()=='n'||getch()=='N')
 95         add();
 96     else
 97         menu();    
 98 
 99 }
100 
101 void show()
102 {
103     int i;
104     if(num!=0)
105     {
106         printf("\n\t\t*********All the information below**********");
107         for(i=0;i<num;i++)
108         {
109             printf("\n\t\tNo. %d",i+1);
110             printf("\n\t\tName: %s",addr[i].name);
111             printf("\n\t\tID:   %s",addr[i].ID);
112             printf("\n\t\tClass:%s",addr[i].cla);
113             printf("\n\t\tMajor:%s",addr[i].major);
114             printf("\n\t\tTel:  %s",addr[i].tel);
115             printf("\n\t\tAge:  %s",addr[i].age);
116             printf("\n\t\tBirth:%s",addr[i].birth);
117             printf("\n\t\tEmail:%s",addr[i].email);
118             printf("\n\t\tDom:  %s",addr[i].dom);
119             printf("\n\t\tHome: %s\n\n",addr[i].home);
120         }
121      system("pause");
122        }
123     else 
124     {
125         printf("\n\t\tThere is no record!\n");
126         system("pause");
127     }
128     menu();    
129 }
130 
131 void modify()
132 {
133                 
134 }
135 
136 void del()
137 {
138     char s[20];
139     int i=0,j;
140     if(num==0)
141     {
142         printf("\n\t\tThere is no records!");
143         getch();
144         menu();
145     }
146     printf("\n\t\tPlease enter the name you want to delete:");
147     scanf("%s",s);
148     for(j=0;j<=num-1;j++)
149     {
150         if(strcmp(addr[j].name,s)!=0)
151             {
152                 if(j==num)
153                 {
154                     printf("\n\t\tError!didn't find the records.");
155                     continue;
156                 } 
157             }
158     
159         else
160         {
161             strcpy(addr[j].name,addr[j+1].name);
162             strcpy(addr[j].ID,addr[j+1].ID);
163             strcpy(addr[j].cla,addr[j+1].cla);
164             strcpy(addr[j].major,addr[j+1].major);
165             strcpy(addr[j].tel,addr[j+1].tel);
166             strcpy(addr[j].age,addr[j+1].age);
167             strcpy(addr[j].birth,addr[j+1].birth);
168             strcpy(addr[j].email,addr[j+1].email);
169             strcpy(addr[j].dom,addr[j+1].dom);
170             strcpy(addr[j].home,addr[j+1].home);
171             num--; 
172             printf("\n\t\tDelete successfully!");    
173         }
174     } 
175     printf("\n\t\tEnd deleting?(Y/N):");
176     if(getch()=='n'||getch()=='N')
177           del();    
178     else
179         menu();     
180         
181         
182 }
183 
184 
185 <strong>void save()</strong>
186 {
187     FILE *fp;
188     int i;
189     if((fp=fopen("addressbook.dat","wb"))==NULL)
190     {
191         printf("\n\t\tOpen fail!");
192         exit(0);
193     }
194     else
195     printf("success!");
196     for(i=0;i<=num;i++)    
197     {
198     if((fwrite(&addr[i],sizeof(struct Addrbook),1,fp))!=1)
199         {    
200             printf("%s",addr[i].name);
201         }
202     }
203     fclose(fp);
204     printf("\n\t\tPress any keys to get back to the menu.");
205     getch();
206     menu();
207 }
208 
209 
210 <strong>void read()</strong>
211 {
212     FILE *fp;
213     int i;
214     if((fp=fopen("addressbook.dat","rb"))==NULL)
215         {    
216             printf("\n\t\tOpen fail!");
217             system("pause");
218             getch();
219             menu();
220         }
221     else
222         printf("success!");    
223         system("pause");
224         for(i=0;i<num;i++)    
225          {
226             if(fread(&addr[i],LEN,1,fp)!=1)
227                 printf("\n\t\tSomething wrong!\n");            
228         }
229     fclose(fp);
230 }
陈玩玩的主页 陈玩玩 | 初学一级 | 园豆:143
提问于:2015-05-24 21:41
< >
分享
最佳答案
1

你可以调试一下,在第几行出错了。我粘贴代码都把行号粘贴出来了,不好调试。你把源代码干净的贴出来。不要行号。

收获园豆:20
[0] | 小虾三级 |园豆:1257 | 2015-05-24 21:53

大概就是fwrite那里应该是有问题,但是在别的程序里写的时候行得通,,,这里QAQ麻烦你看一下,,,

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define LEN sizeof(struct Addrbook)

struct Addrbook{
int num;
char name[20];
char ID[20];
char cla[20];
char major[20];
char tel[20];
char age[10];
char birth[10];
char email[30];
char dom[30];
char home[30];
}addr[100];

int num=0; 

void add();
void show();
void modify();
void del();
void menu();
void save();
void read();

int main()
{
read();
show();
menu();
return 0; 
}


void menu()
{
system("cls");
printf("\n\t\t***********************MENU************************");
printf("\n\t\t 1--Add 2--Show ");
printf("\n\t\t 3--Modify 4--Delete ");
printf("\n\t\t 5--Save 6--Exit ");
char n;
printf("\n\t\tEnter the number:");
scanf("%s",&n);
fflush(stdin);
switch(n)
{
case '1':add();break;
case '2':show();break;
case '3':modify();break;
case '4':del();break;
case '5':save();break;
case '6':exit(0);
default:printf("Error!");break;
}
}


void add()
{
printf("\n\t\t********Please enter the information*******");
printf("\n\t\tNo.%d",num+1);
printf("\n\t\tName: ");
scanf("%s",&addr[num].name);
printf("\t\tID: ");
scanf("%s",&addr[num].ID);
printf("\t\tClass ");
scanf("%s",&addr[num].cla);
printf("\t\tMajor:");
scanf("%s",&addr[num].major);
printf("\t\tTel: ");
scanf("%s",&addr[num].tel);
printf("\t\tAge: ");
scanf("%s",&addr[num].age);
printf("\t\tBirth:");
scanf("%s",&addr[num].birth);
printf("\t\tEmail:");
scanf("%s",&addr[num].email);
printf("\t\tDom: ");
scanf("%s",&addr[num].dom);
printf("\t\tHome: ");
scanf("%s",&addr[num].home);
num++;

printf("\n\t\tEnd adding?(Y/N):");
if(getch()=='n'||getch()=='N')
add();
else
menu();

}

void show()
{
int i;
if(num!=0)
{
printf("\n\t\t*********All the information below**********");
for(i=0;i<num;i++)
{
printf("\n\t\tNo. %d",i+1);
printf("\n\t\tName: %s",addr[i].name);
printf("\n\t\tID: %s",addr[i].ID);
printf("\n\t\tClass:%s",addr[i].cla);
printf("\n\t\tMajor:%s",addr[i].major);
printf("\n\t\tTel: %s",addr[i].tel);
printf("\n\t\tAge: %s",addr[i].age);
printf("\n\t\tBirth:%s",addr[i].birth);
printf("\n\t\tEmail:%s",addr[i].email);
printf("\n\t\tDom: %s",addr[i].dom);
printf("\n\t\tHome: %s\n\n",addr[i].home);
}
system("pause");
}
else 
{
printf("\n\t\tThere is no record!\n");
system("pause");
}
menu(); 
}

void modify()
{

}

void del()
{
char s[20];
int i=0,j;
if(num==0)
{
printf("\n\t\tThere is no records!");
getch();
menu();
}
printf("\n\t\tPlease enter the name you want to delete:");
scanf("%s",s);
for(j=0;j<=num-1;j++)
{
if(strcmp(addr[j].name,s)!=0)
{
if(j==num)
{
printf("\n\t\tError!didn't find the records.");
continue;
} 
}

else
{
strcpy(addr[j].name,addr[j+1].name);
strcpy(addr[j].ID,addr[j+1].ID);
strcpy(addr[j].cla,addr[j+1].cla);
strcpy(addr[j].major,addr[j+1].major);
strcpy(addr[j].tel,addr[j+1].tel);
strcpy(addr[j].age,addr[j+1].age);
strcpy(addr[j].birth,addr[j+1].birth);
strcpy(addr[j].email,addr[j+1].email);
strcpy(addr[j].dom,addr[j+1].dom);
strcpy(addr[j].home,addr[j+1].home);
num--; 
printf("\n\t\tDelete successfully!"); 
}
} 
printf("\n\t\tEnd deleting?(Y/N):");
if(getch()=='n'||getch()=='N')
del(); 
else
menu(); 


}


void save()
{
FILE *fp;
int i;
if((fp=fopen("addressbook.dat","wb"))==NULL)
{
printf("\n\t\tOpen fail!");
exit(0);
}
else
printf("success!");
for(i=0;i<=num;i++) 
{
if((fwrite(&addr[i],sizeof(struct Addrbook),1,fp))!=1)
{ 
printf("%s",addr[i].name);
}
}
fclose(fp);
printf("\n\t\tPress any keys to get back to the menu.");
getch();
menu();
}


void read()
{
FILE *fp;
int i;
if((fp=fopen("addressbook.dat","rb"))==NULL)
{ 
printf("\n\t\tOpen fail!");
system("pause");
getch();
menu();
}
else
printf("success!"); 
system("pause");
for(i=0;i<num;i++) 
{
if(fread(&addr[i],LEN,1,fp)!=1)
printf("\n\t\tSomething wrong!\n"); 
}
fclose(fp);
}

 

陈玩玩 | 园豆:143 (初学一级) | 2015-05-24 21:57

@Suzy_C: 我运行了一下,是不是add以后,save以后,再次运行程序的时候,显示没有记录啊?是不是?但是查看文件,发现确实保存了。是不是这个问题?程序没有bug,就是有一些逻辑问题。在我这里,可以运行的。

[0] | 园豆:1257 (小虾三级) | 2015-05-24 22:23

@千紫小星: 输入数据再关闭,save以后再选2.show会有之前输入的数据显示出来吗?

陈玩玩 | 园豆:143 (初学一级) | 2015-05-24 22:28

@Suzy_C: 

[0] | 园豆:1257 (小虾三级) | 2015-05-24 22:34

@Suzy_C: 你先看看我的贴图,你这个逻辑对吗?还是我哪里没有注意到。

[0] | 园豆:1257 (小虾三级) | 2015-05-24 22:35

@Suzy_C: 还有就是,你的读写文件的方式不对,现在你的方式,每次添加新的记录,就会把以前的记录覆盖掉,你的文件里面永远是上次运行程序时添加的记录。仔细看下书,这点书中有介绍的。

[0] | 园豆:1257 (小虾三级) | 2015-05-24 22:52

@千紫小星: 我加了个7.read选项,结果每次选完7,记录就没了。。。可是哪里错了呢?

陈玩玩 | 园豆:143 (初学一级) | 2015-05-24 23:48

@Suzy_C: 大概还是num没弄好,,,我已经知道错哪儿了,接下来慢慢改吧,谢谢ಥ_ಥ

陈玩玩 | 园豆:143 (初学一级) | 2015-05-24 23:50

@千紫小星: 终于弄对了ಥ_ಥ蟹蟹!

陈玩玩 | 园豆:143 (初学一级) | 2015-05-25 00:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册