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

为什么 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
    评论
    解决 无用 1
    打赏 举报

相关推荐 更多相似问题