在vc里用fopen("log.txt","a")当log.txt存在时GetLastError返回183

解决方案 »

  1.   

    你使用a+打开方式看看
    fopen("log.txt","a+")
    183返回值表示
    ERROR_ALREADY_EXISTS
    Cannot create a file when that file already exists. 
      

  2.   

    "r"Opens for reading. If the file does not exist or cannot be found, the fopen call fails."w"Opens an empty file for writing. If the given file exists, its contents are destroyed."a"Opens for writing at the end of the file (appending) without removing the EOF er before writing new data to the file; creates the file first if it doesn’t exist."r+"Opens for both reading and writing. (The file must exist.)"w+"Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed."a+"Opens for reading and appending; the appending operation includes the removal of the EOF er before new data is written to the file and the EOF er is restored after writing is complete; creates the file first if it doesn’t exist./* FOPEN.C: This program opens files named "data"
     * and "data2".It  uses fclose to close "data" and
     * _fcloseall to close all remaining files.
     */#include <stdio.h>FILE *stream, *stream2;void main( void )
    {
       int numclosed;   /* Open for read (will fail if file "data" does not exist) */
       if( (stream  = fopen( "data", "r" )) == NULL )
          printf( "The file 'data' was not opened\n" );
       else
          printf( "The file 'data' was opened\n" );   /* Open for write */
       if( (stream2 = fopen( "data2", "w+" )) == NULL )
          printf( "The file 'data2' was not opened\n" );
       else
          printf( "The file 'data2' was opened\n" );   /* Close stream */
       if( fclose( stream ) )
          printf( "The file 'data' was not closed\n" );   /* All other files are closed: */
       numclosed = _fcloseall( );
       printf( "Number of files closed by _fcloseall: %u\n", numclosed );
    }
     所以说,不存在1楼所说的问题。
      

  3.   

    用了"a"参数还返回Already Exist的错误?
      

  4.   

    没人试试吗,用"a+”也一样,是不是ms的bug啊
      

  5.   

    fopen("log.txt","a")当log.txt存在时GetLastError返回183//fopen失败,GetLastError得到的值未必就是CreateFile的错误。
    //直接用CreateFile看看。