循环遍历C中文件的每一行 [英] looping over each line of a file in C

查看:164
本文介绍了循环遍历C中文件的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FILE 

我想知道如何循环遍历文件的每一行。 * todoFile;
todoFile = fopen(./ todo.txt,r); (todoFile!= NULL){

} else {
printf(ERROR);

if


解决方案

文件一行一行,直到它结束是

$ $ p $ $ code假设行是一个char数组* /
while( fgets(line,sizeof line,handle)){
size_t len = strlen(line);
if(len&& line [len - 1]!='\\\
')){
/ *不完整的行* /
}
/ *可能删除尾随换行符和* /
/ *处理行* /
}


I'm wondering how you can loop over each line of a file heres the code I have so far:

FILE *todoFile;
todoFile = fopen("./todo.txt", "r");

if (todoFile != NULL) {

} else {
    printf("ERROR");
}

解决方案

The idiomatic way to read a file line-by-line until it ends is

    /* 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 */
    }

这篇关于循环遍历C中文件的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆