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

为什么 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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛