我有一个文本文件,我想逐行读取,并将行放入数组中。
后面的代码段在编译时出错:
FILE *f;
char line[LINE_SIZE];
char *lines;
int num_righe;
f = fopen("spese.dat", "r");
if(f == NULL) {
f = fopen("spese.dat", "w");
}
while(fgets(line, LINE_SIZE, f)) {
num_righe++;
lines = realloc(lines, (sizeof(char)*LINE_SIZE)*num_righe);
strcpy(lines[num_righe-1], line);
}
fclose(f);
错误是:
spese.c:29: warning: assignment makes integer from pointer without a cast
spese.c:30: warning: incompatible implicit declaration of built-in function ‘strcpy’
spese.c:30: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
有什么帮助吗?
谢谢
最佳答案
尝试:
FILE *f;
char line[LINE_SIZE];
char **lines = NULL;
int num_righe = 0;
f = fopen("spese.dat", "r");
if(f == NULL) {
f = fopen("spese.dat", "w");
}
while(fgets(line, LINE_SIZE, f)) {
num_righe++;
lines = (char**)realloc(lines, sizeof(char*)*num_righe);
lines[num_righe-1] = strdup(line);
}
fclose(f);
关于c - C:将文件读入数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/821602/