void readfile() // 定义文件初始化函数 { int j; FILE *fp; if((fp=fopen("d:\\PEOPLE.txt","r"))==NULL) { printf("Connot open file strike any key exit!\n"); exit(0); } else { for(j=0;fread(&peo[j],sizeof(struct meeting),1,fp)!=0;j++);//从文件中读取数据 len=j; for(i=0;i<len;i++) { strcpy(peo[len].name,peo[j].name); strcpy(peo[len].tele,peo[j].tele); strcpy(peo[len].ID,peo[j].ID); strcpy(peo[len].department,peo[j].department); strcpy(peo[len].room_num,peo[j].room_num); }//把文件中的数据复制到结构体中 } fclose(fp);//关闭文件 }
时间: 2024-04-03 13:35:44 浏览: 11
这段代码实现了读取文件并初始化结构体数组的功能。具体来说,它打开了名为“PEOPLE.txt”的文件,读取其中的数据并将其存储在一个名为“peo”的结构体数组中。该数组的每个元素都包含了一个名为“name”的字符串、一个名为“tele”的字符串、一个名为“ID”的字符串、一个名为“department”的字符串和一个名为“room_num”的字符串。在读取文件时,它使用了一个循环来遍历文件中的所有记录,并计算出记录的总数。最后,它将数组中的数据复制到另一个数组中,并关闭了文件。
需要注意的是,该代码中的“struct meeting”类型没有在代码中给出,因此我们无法确定结构体的完整定义。此外,在将文件中的数据复制到结构体数组中时,它使用了错误的索引值(应该是“i”而不是“len”),这可能导致数据被复制到错误的位置。
相关问题
#include<stdio.h>#include<string.h>#include<stdlib.h>#include <conio.h>#define N 100 struct subway//定义一个结构体 { char num[N]; char line[N];//地铁线路 char sstation[N];//起始站 char ostation[N];//终点站 char wstation[N];//途径站数 char charge[N];//票价 }sub[N];//给结构体起别名sub; int k=1,n,m; char readfile();//文件读取 void save();//将信息保存至文件 void insert();//增加地铁信息 void Delete();//删除地铁信息 void Search();//查询地铁信息 void Print();//浏览地铁信息 void Update();//更新地铁信息 void menu();//主菜单调用其他函数 int main(){ while(k) menu(); system("pause"); return 0;} char readfile(){ FILE *fp;//要读取的文件指针 int i=0;//数组移动 if ((fp=fopen("subway.txt","r"))==NULL)//打开操作不成功 { printf("Open file is error!"); system("pause"); exit(0); }
//文件打开成功
while(!feof(fp)){//feof函数判断是否到达文件末尾
fscanf(fp,"%s",sub[i].num);//读取地铁编号
fscanf(fp,"%s",sub[i].line);//读取地铁线路
fscanf(fp,"%s",sub[i].sstation);//读取起始站
fscanf(fp,"%s",sub[i].ostation);//读取终点站
fscanf(fp,"%s",sub[i].wstation);//读取途径站数
fscanf(fp,"%s",sub[i].charge);//读取票价
i++;
}
fclose(fp);//关闭文件
return 'r';//返回'r'表示读取成功
}
void save(){
FILE *fp;//要写入的文件指针
int i;
if ((fp=fopen("subway.txt","w"))==NULL)//打开操作不成功
{
printf("Open file is error!");
system("pause");
exit(0);
}
for(i=0;i<n;i++){
fprintf(fp,"%s %s %s %s %s %s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);//将信息写入文件中
}
fclose(fp);//关闭文件
printf("Save successfully!\n");
}
void insert(){
printf("Please input the number of subway:");
scanf("%s",sub[n].num);
printf("Please input the line of subway:");
scanf("%s",sub[n].line);
printf("Please input the start station of subway:");
scanf("%s",sub[n].sstation);
printf("Please input the end station of subway:");
scanf("%s",sub[n].ostation);
printf("Please input the passing stations of subway:");
scanf("%s",sub[n].wstation);
printf("Please input the charge of subway:");
scanf("%s",sub[n].charge);
n++;//增加地铁信息数量
printf("Insert successfully!\n");
}
void Delete(){
int i,j;
char num[N];//要删除的地铁编号
printf("Please input the number of subway you want to delete:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要删除的地铁信息
for(j=i;j<n-1;j++){
sub[j]=sub[j+1];//将后面的信息前移
}
n--;//减少地铁信息数量
printf("Delete successfully!\n");
return;
}
}
printf("Can't find the subway information!\n");
}
void Search(){
int i;
char num[N];//要查询的地铁编号
printf("Please input the number of subway you want to search:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要查询的地铁信息
printf("Number:%s\tLine:%s\tStart Station:%s\tEnd Station:%s\tPassing Stations:%s\tCharge:%s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);
return;
}
}
printf("Can't find the subway information!\n");
}
void Print(){
int i;
for(i=0;i<n;i++){
printf("Number:%s\tLine:%s\tStart Station:%s\tEnd Station:%s\tPassing Stations:%s\tCharge:%s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);
}
}
void Update(){
int i;
char num[N];//要更新的地铁编号
printf("Please input the number of subway you want to update:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要更新的地铁信息
printf("Please input the new line of subway:");
scanf("%s",sub[i].line);
printf("Please input the new start station of subway:");
scanf("%s",sub[i].sstation);
printf("Please input the new end station of subway:");
scanf("%s",sub[i].ostation);
printf("Please input the new passing stations of subway:");
scanf("%s",sub[i].wstation);
printf("Please input the new charge of subway:");
scanf("%s",sub[i].charge);
printf("Update successfully!\n");
return;
}
}
printf("Can't find the subway information!\n");
}
void menu(){
printf("******************************************\n");
printf("* Subway System *\n");
printf("* *\n");
printf("* 1.Read file *\n");
printf("* 2.Save *\n");
printf("* 3.Insert *\n");
printf("* 4.Delete *\n");
printf("* 5.Search *\n");
printf("* 6.Print *\n");
printf("* 7.Update *\n");
printf("* 0.Exit *\n");
printf("* *\n");
printf("******************************************\n");
printf("Please input the number of function you want to execute:");
scanf("%d",&m);
switch(m){
case 1:if(readfile()=='r') printf("Read file successfully!\n");break;
case 2:save();break;
case 3:insert();break;
case 4:Delete();break;
case 5:Search();break;
case 6:Print();break;
case 7:Update();break;
case 0:k=0;break;
default:printf("Input error! Please input again!\n");break;
}
}
void ReadFile() { FILE* fp; long len; int i; if ((fp = fopen("fam. dat", "rb")) == NULL) n = 0; return; } fseek(fp, 0, 2); len = ftell(fp); rewind(fp); n = len / sizeof(FamType); for (i = 0; i < n; i++) fread(&fam[i], sizeof(FamType), 1, fp); void SaveFile() { int i,n; FILE* fp; if ((fp = fopen("fam. dat", "wb")) == NULL) { printf("\n >>数据家谱文件不能打开\n\n"); return; } for (i = 0; i < n; i++) fwrite(&fam[i], sizeof(FamType), 1, fp); fclose(fp); void InputFam() { printf("\n >>输入父亲、母亲和儿子姓名:\n"); scanf("%s%s%s", fam[n].father, fam[n].wife, fam[n].son); n++; } void outputFile() { int i,n; if (n <= 0) { printf("\n >>没有任何记录\n"); return; } printf("\n 父亲 母亲 儿子\n"); printf(" \n"); for (i = 0; i < n; i++) printf(" %10s%10s%10s\n", fam[i].father, fam[i].wife, fam[i].son); printf(" \n\n"); }改错
这段代码有很多错误,我来一一指出并修正:
1. `ReadFile()` 函数中的 `return` 语句应该放在 `if` 语句内部,否则无论文件是否成功打开,函数都会直接返回。
2. 在 `ReadFile()` 函数中,读取文件之前应该检查文件是否已经成功打开,因为如果文件打开失败,后面的操作都无法进行。
3. 在 `SaveFile()` 函数中,变量 `n` 没有被赋值,应该在函数开始处定义并初始化。
4. 在 `SaveFile()` 函数中,写入文件之前应该检查文件是否已经成功打开,因为如果文件打开失败,后面的操作都无法进行。
5. 在 `InputFam()` 函数和 `outputFile()` 函数中,变量 `n` 没有被定义。
6. 在 `outputFile()` 函数中,输出表格的格式有误,应该先输出表头。
修正后的代码如下:
```
void ReadFile() {
FILE* fp;
long len;
int i;
if ((fp = fopen("fam.dat", "rb")) == NULL) {
n = 0;
return;
}
fseek(fp, 0, 2);
len = ftell(fp);
rewind(fp);
n = len / sizeof(FamType);
for (i = 0; i < n; i++)
fread(&fam[i], sizeof(FamType), 1, fp);
fclose(fp);
}
void SaveFile() {
int i, n = ::n;
FILE* fp;
if ((fp = fopen("fam.dat", "wb")) == NULL) {
printf("\n >>数据家谱文件不能打开\n\n");
return;
}
for (i = 0; i < n; i++)
fwrite(&fam[i], sizeof(FamType), 1, fp);
fclose(fp);
}
void InputFam() {
printf("\n >>输入父亲、母亲和儿子姓名:\n");
scanf("%s%s%s", fam[n].father, fam[n].wife, fam[n].son);
n++;
}
void outputFile() {
int i, n = ::n;
if (n <= 0) {
printf("\n >>没有任何记录\n");
return;
}
printf("\n 父亲 母亲 儿子\n");
printf(" ============================\n");
for (i = 0; i < n; i++)
printf(" %10s%10s%10s\n", fam[i].father, fam[i].wife, fam[i].son);
printf(" ============================\n\n");
}
```