我想逐行读取文件中的内容,但是尝试过程中出现了问题。
以下是我的尝试:
FILE *file;
char *line = NULL;
int len = 0;
char read;
file=fopen(argv[1], "r");
if (file == NULL)
return 1;
while ((read = getline(&line, len, file)) != -1) {
printf("Retrieved line of length %s :\n", &read);
printf("%s", line);
}
if (line)
free(line);
return 0;
有什么建议,为什么它不起作用?
getline()
不是这样工作的。它为行本身分配内存。 - user529758getline
函数的第二个参数是错误的。它需要一个指针而不是一个整数。 - Jens Gustedt