日期:2014-05-16  浏览次数:21574 次

fopen 打开已存在的文件,报 No such file or directory
 
  在 suse linux server 11 服务器上, 在 vsftpd 程序中,增加了一段代码写临时日志文件,便于跟踪调试,
  但在程序执行到写日志的代码时, 报错 No such file or directory

  在没有生成这个日志文件时, 与 手动新建这个日志文件,两种情况下,都报出了这个错.



  写日志代码如下 

  FILE* fp = fopen("/var/log/test.txt","w");
  if(fp != NULL)
  {
  fprintf(fp,"%s","begin to trace!");
  fclose(fp);
  }
 

  程序运行的环境说明如下:

  vsftpd 程序的用户是 root

  vsftpd 程序布署的目录是 /usr/sbin  
  sbin 目录属性 drwxrwxrwx 2 root root 16384 Aug 3 16:57 sbin
  var 目录属性 drwxrwxrwx 14 root root 4096 Mar 9 11:11 usr

  日志文件 test.txt  
  保存的目录是 /var/log/

  var 目录属性 drwxrwxrwx 16 root root 4096 Mar 9 11:13 var
  log 目录属性 drwxrwxrwx 13 root root 4096 Aug 3 15:45 log  



  在 var/log/ 下执行 ls -ltr test.txt 结果如下

  ZXKF3:/var/log # ls -ltr test.txt
  -rwxrwxrwx 1 root root 0 Aug 3 16:24 test.txt  


  想请大侠们帮忙分析下 ,问题的原因在哪?  



   



   
 

------解决方案--------------------
写日志一般采用 a+ 模式


r Open text file for reading. The stream is positioned at the beginning
of the file.

r+ Open for reading and writing. The stream is positioned at the
beginning of the file.

w Truncate file to zero length or create text file for writing. The
stream is positioned at the beginning of the file.

w+ Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at the
beginning of the file.

a Open for appending (writing at end of file). The file is created if it
does not exist. The stream is positioned at the end of the file.

a+ Open for reading and appending (writing at end of file). The file is
created if it does not exist. The initial file position for reading is
at the beginning of the file, but output is always appended to the end
of the file.
------解决方案--------------------
自己动手丰衣足食,接分