第一行后stdin到达EOF

时间:2019-07-08 18:40:40

标签: c std stdin fgets eof

我正在尝试使用type file.txt | myprogram.exe将文件通过管道传输到我的程序。我正在阅读:

char *line;
struct block *newblock, *cur;
int i, osl, cbs, rs;
int startblock;

/* read in the blocks ... */
while (!feof(stdin) && !ferror(stdin)) {
    cbs = 2048;
    line = (char *)malloc(cbs + 1);
    if (!line) {
        perror("malloc");
        exit(1);
    }
    line[cbs] = '\0';

    /* read in the line */
    if (!fgets(line, cbs, stdin)) {
        free(line);
        continue;
    } else {
        while (line[cbs - 2]) {
            /* expand the line */
            line = (char *)realloc(line, (cbs * 2) + 1);
            if (!line) {
                perror("realloc");
                exit(1);
            }
            line[cbs * 2] = '\0';

            /* read more */
            if (!fgets(line + cbs - 1, cbs + 1, stdin))
                break;

            cbs *= 2;
        }
    }
    ...
}

但是在读取第一行之后,即使文件中有多行,feof()也会返回true。怎么会这样

1 个答案:

答案 0 :(得分:1)

代码有多个问题:

  • 文件结尾的测试不正确:<div ng-controller="safeCtrl" class="table_class"> <table st-table="displayedCollection" st-safe-src="rowCollection" class="table table-striped"> <thead> <tr> <th st-ratio="20">#</th> <th st-sort="Delivered" style="color: white;">Delivered</th> <th st-sort="orgRadar" style="color: white;">Organization Radar</th> <th st-sort="root_build" style="color: white;">Root Build</th> <th st-sort="inserted_by" style="color: white;">Inserted By</th> <th st-sort="milestone" style="color: white;">Milestone</th> <th st-sort="applicable_chipsets" style="color: white;">Applicable Chipsets</th> <th st-sort="project_tag" style="color: white;">Project Tag</th> <th st-sort="gerrits" style="color: white;">Gerrits</th> <th st-sort="inserted_on" style="color: white;">Inserted On</th> <th st-sort="SDK" style="color: white;">SDK</th> </tr> <tr> <th colspan="10"><input st-search="" class="form-control" placeholder="global search ..." type="text" /></th> </tr> </thead> <tbody> <tr ng-repeat="row in displayedCollection" *ngFor="let item of newPost.posts"> <td> <button ng-if="row.expanded" ng-click="row.expanded = false" class="btn btn-xs btn-default glyphicon glyphicon-chevron-down"></button> <button ng-if="!row.expanded" ng-click="row.expanded = true" class="btn btn-xs btn-default glyphicon glyphicon-chevron-right"></button> </td> <td style="color: red;">{{item.Delivered}}</td> <td style="color: red;">{{item.orgRadar}}</td> <td style="color: red;">{{item.root_build}}</td> <td style="color: red;">{{item.inserted_by}}</td> <td style="color: red;">{{item.milestone}}</td> <td style="color: red;">{{item.applicable_chipsets}}</td> <td style="color: red;">{{item.project_tag}}</td> <td style="color: red;">{{item.gerrits}}</td> <td style="color: red;">{{item.inserted_on}}</td> <td style="color: red;">{{item.SDK}}</td> <td> <button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger"> <i class="glyphicon glyphicon-remove-circle"> </i> </button> </td> </tr> <tr ng-if="row.expanded" ng-repeat-end=""> <td colspan="10"> <b>Original Radar<br /></b> </td> <td colspan="10" st-ratio="20">{{item.orgRadar}}</td> </tr> </tbody> </table> </div> 仅在读取功能失败后提供有意义的信息。
  • 重新分配方案已损坏:您测试了数组的倒数第二个字节,但是如果行较短,feof()可能没有设置该字节。

这是修复程序的方法:

  • 如果系统支持,请使用fgets()getline()分配了足够大的缓冲区以一次读取整行。
  • 或使用getline()读取行片段并检查其是否以换行符结尾。