C 程序有什么问题?
节目迷们,请帮帮我,提前谢谢。该文件可在下面下载C 程序有什么问题?,c,file,C,File,节目迷们,请帮帮我,提前谢谢。该文件可在下面下载 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int deed_id; char deed_name[55]; int points_for_deed; int total_times_done; int total_points_earned; } deed;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int deed_id;
char deed_name[55];
int points_for_deed;
int total_times_done;
int total_points_earned;
} deed;
int main(){
FILE *file1;
file1=fopen("deed_list.txt", "r");
if(file1==NULL){
printf("Can not open the file");
return 1;
}
int j;
fscanf(file1, "%i", &j);
deed **deed_list = (deed**)malloc(sizeof(deed)*j);
int i;
for(i=0; i<j; i++){
fscanf(file1, "%i %s %i", &deed_list[i]->deed_id, deed_list[i]->deed_name, &deed_list[i]->points_for_deed);
}
printf("%i",j);
fclose(file1);
return 0;
}
#包括
#包括
#包括
类型定义结构{
国际契约id;
char契据_名称[55];
契约的积分;
完成的总次数;
int获得的总积分;
}契约;
int main(){
文件*file1;
file1=fopen(“契约列表”txt,“r”);
if(file1==NULL){
printf(“无法打开文件”);
返回1;
}
int j;
fscanf(文件1、%i、&j);
契约**契约清单=(契约**)malloc(契约)*j的规模);
int i;
对于(i=0;id、契约列表[i]->契约名称和契约列表[i]->契约点数);
}
printf(“%i”,j);
fclose(文件1);
返回0;
}
您不希望指针指向存储契约数组的指针,请更改为单个指针(并且不要强制转换malloc
):
别忘了打免费电话(契约清单)代码>结尾。契约**契约列表=(契约**)malloc(契约)*j代码>-->契约*契约列表=malloc(契约的大小)*j)代码>,&契约列表[i]->契约id
-->&契约列表[i]。契约id
等等。因此不是调试服务。使用符号编译,在调试器中运行代码,逐行跟踪程序,检查相关变量的值,了解实际情况。如果出现了一个特别的问题,欢迎回到这里。同样在C语言中,malloc&friends是无用的。
deed **deed_list = (deed**)malloc(sizeof(deed)*j);
deed *deed_list = malloc(sizeof(deed)*j);