Visual Studio向我显示如下警告:
警告C4133:'=':不兼容的类型 - 从'FILE *'到'char *'
警告C4133:'function':不兼容的类型 - 从'char *'到'FILE *'
警告在注释“此行”的行上。我该如何解决?
我的代码:
typedef struct {
int numberOfCity;
tListOfCity* list;
double** distances;
} tDatabase;
tDatabase* LoadDatabase(char* file) {
FILE* file;
tDatabase *input;
char data[100];
int i, j, number = 0;
input = (tDatabase*) calloc(1, sizeof (tDatabase));
input->list = NULL;
file = fopen("file.txt", "r"); //this line
fscanf(file, "%d", &number); //this line
input->numberOfCity = number;
input->distances = (double**) calloc(data->numberOfCity, sizeof (double*));
for (i = 0; i < input->numberOfCity; i++) {
input->distances[i] = (double*) calloc(input->numberOfCity, sizeof (double));
}
for (i = 0; i <= input->numberOfCity; i++) {
fgets(data, sizeof (data), file); //this line
input->list = AddOnEnd(input->list, CreateCity(data));
}
for (i = 0; i < input->numberOfCity; i++) {
for (j = 0; j < input->numberOfCity; j++) {
fscanf(file, "%f", &input->distances[i][j]); //this line
}
fclose(file);
return input;
}
答案 0 :(得分:3)
代码中有两个file
个变量。
首先是作为参数传入的内容,另一个是FILE * file
。将参数更改为fileName
或类似的内容,并修补代码以反映该参数。
tDatabase* LoadDatabase(char* fileName) {
FILE* file;
tDatabase *input;
char data[100];
int i, j, number = 0;
input = (tDatabase*) calloc(1, sizeof (tDatabase));
input->list = NULL;
file = fopen("file.txt", "r"); //this line
fscanf(file, "%d", &number); //this line
input->numberOfCity = number;
input->distances = (double**) calloc(data->numberOfCity, sizeof (double*));
for (i = 0; i < input->numberOfCity; i++) {
input->distances[i] = (double*) calloc(input->numberOfCity, sizeof (double));
}
for (i = 0; i <= input->numberOfCity; i++) {
fgets(data, sizeof (data), file); //this line
input->list = AddOnEnd(input->list, CreateCity(data));
}
for (i = 0; i < input->numberOfCity; i++) {
for (j = 0; j < input->numberOfCity; j++) {
fscanf(file, "%f", &input->distances[i][j]); //this line
}
fclose(file);
return input;
}
答案 1 :(得分:2)
您不应该为参数和局部变量指定相同的名称:
tDatabase* LoadDatabase(char* file) {
FILE* file;
也许这是一个很好的机会让你的函数尊重它作为参数获得的文件名(现在它总是打开“file.txt”)。