| 
 | 
 
 
发表于 2023-1-24 21:22:24
|
显示全部楼层
 
 
 
这程序没有问题吧?是不是缓冲区太小,溢出了? 
你是如何判断出  是调用fclose报错的? 
 
- sh-5.1$ cat main.c
 
 - #include <stdio.h>
 
 - #include <string.h>
 
 - #include <stdlib.h>
 
  
- int main() {
 
 -     char *home = malloc(sizeof(char) * 1024);
 
 -     strcpy(home, getenv("HOME"));
 
  
-     char *abc1 = malloc(sizeof(char) * 1024);
 
 -     char *abc2 = malloc(sizeof(char) * 1024);
 
  
-     sprintf(abc1, "%s/goal", home);
 
 -     FILE *abc3 = fopen(abc1, "r+");
 
 -     if(NULL == abc3) {
 
 -         fprintf(stderr, "open file failed.L1\n");
 
 -         // memory leaks
 
 -         free(abc2);
 
 -         free(abc1);
 
 -         free(home);
 
 -         return -1;
 
 -     }
 
  
-     fscanf(abc3, "%s", abc2);
 
 -     fprintf(stdout, "abc2==%s\n", abc2);
 
 -     fclose(abc3);
 
  
-     free(home);
 
 -     home = NULL;
 
 -     free(abc1);
 
 -     abc1 = NULL;
 
 -     free(abc2);
 
 -     abc2 = NULL;
 
 -     return 0;
 
 - }
 
 - sh-5.1$ ./main
 
 - abc2==hello
 
 - sh-5.1$
 
 
  复制代码 |   
 
 
 
 |