我想知道如何循环遍历文件的每一行,这是我到目前为止的代码:
FILE *todoFile;
todoFile = fopen("./todo.txt", "r");
if (todoFile != NULL) {
} else {
printf("ERROR");
}
Run Code Online (Sandbox Code Playgroud)
逐行读取文件的惯用方法是
/* assume line is a char array */
while (fgets(line, sizeof line, handle)) {
size_t len = strlen(line);
if (len && (line[len - 1] != '\n')) {
/* incomplete line */
}
/* possibly remove trailing newline ... and */
/* deal with line */
}
Run Code Online (Sandbox Code Playgroud)