larry*wei 2018-10-29 02:39 采纳率: 0%
浏览 1798

为什么 fgets 持续地返回 NULL?

I'm trying to write a program that can open all given files in a directory, and output the files with some changes to another directory given a path specified by the user. I made a function that will be called after fork() for each filename encountered in the current directory, and the output will have the same name in this new directory. I have looked all over, and I still can't find a reason as to why fgets() keeps returning NULL.

void sorter(char *fileName, char *directory, char*   newName){

    FILE *edit = fopen(fileName, "r");
    char buf[700];
    char *bufp = buf;
    char *fLine = fgets(bufp, sizeof(buf), edit);

    if (edit == NULL){
        exit(EXIT_FAILURE);
    }

    printf("%s\n", fLine);
    chdir(directory);
    FILE *output=fopen(newName, "w");   
    while(fLine){
        fprintf(output, fLine);
        fprintf(output, "done");    
    }   
    fclose(output);
    fclose(edit);
}

转载于:https://stackoverflow.com/questions/53038017/why-does-fgets-continuousely-return-null

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2018-10-31 14:44
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!