[Case/BugSolve] Unable to open 'libc-start.c': File not found...

在linux上写了一个程序, 运行到main函数结束为止一切正常, 但是到了main函数的return 0的时候, 程序没有退出, vscode提示 "unable to open 'libc-start.c': file not found ...".

上网找了一些解决方法, 但是没有和我的情况适合的.

... debug了一会之后, 意外地发现了导致错误的原因: 文件指针fp被free了, 而不是fclose ... 

(free会导致无法关闭文件?)

void firstLines(Options *opts){
  FileList* fileList = designatedFiles(opts);
  for(int i = 0; i < fileList->count; i++){
    printf("First %d lines of file: %s\n", opts->firstLines, fileList->paths[i]);
    FILE *fp = fopen(fileList->paths[i], "r");
    if(fp == NULL){
      fprintf(stderr, "Error: failed to open file %s\n", fileList->paths[i]);
      continue;
    }

    char tempCh;
    int n_firstLines = opts->firstLines;
    while((tempCh = fgetc(fp)) != EOF && n_firstLines >= 0){
      if(tempCh == '\n') n_firstLines--;
      fprintf(stdout, "%c", tempCh);
    }
// the original code: "free(fp)" fclose(fp); printf(
"\n"); } destroyFileList(fileList); }
posted @ 2021-02-24 23:43  Atosh_Dustosh  阅读(252)  评论(0)    收藏  举报