我现在的问题是:每次打开文件都是从头开始写文件(导致老信息被覆盖),而我希望是“打开一个文件,如果存在,新内容添加在文件尾部;如果文件不存在,则创建文件! HANDLE m_hLogFile = CreateFile(fname,
GENERIC_WRITE,
FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL); WriteFile(m_hLogFile,buf,lstrlen(buf),&num,NULL);
使用了 SetEndOfFile,也无效!那位帅哥有经验的,分享下...

解决方案 »

  1.   

    就是这样用的啊,你打开已经存在的文件之后,调用 SetFilePointerEx 了么?
      

  2.   

    如果用 OVERLAPPED 如果实现呢?
      

  3.   

    CreateFile(xxxx,FILE_FLAG_OVERLAPPED,NULL);
    m_Ol.hEvent = CreateEvent(xxxx);WriteFile(xxxx&m_Ol);  //开始写入数据读就不用说了把
      

  4.   

    用 OVERLAPPED 的话 
    写文件 就要设置OVERLAPPED 
    麻烦的
    直接定位到文件末尾多方便
      

  5.   

    CreateFile(xxxx,FILE_FLAG_OVERLAPPED,NULL);
    m_Ol.hEvent = CreateEvent(xxxx);WriteFile(xxxx&m_Ol); //开始写入数据读就不用说了把
      

  6.   

    那就用 fopen(, "ab");
      

  7.   

    HANDLE m_hLogFile = CreateFile(fname,
            FILE_APPEND_DATA|SYNCHRONIZE,
            FILE_SHARE_READ,
            &sa,
            OPEN_ALWAYS,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
      

  8.   

    SetFilePointer直接将指针定制到文件末尾
      

  9.   

    This function moves the file pointer of an open file. A RAPI version of this function exists, called CeSetFilePointer (RAPI). CopyDWORD SetFilePointer( 
      HANDLE hFile, 
      LONG lDistanceToMove, 
      PLONG lpDistanceToMoveHigh, 
      DWORD dwMoveMethod
    ); Parameters
    hFile 
    [in] Handle to the file whose file pointer is to be moved. The file handle must have been created with GENERIC_READ or GENERIC_WRITE access to the file. 
    lDistanceToMove 
    Low-order 32 bits of a signed value that specifies the number of bytes to move the file pointer. A positive value for lDistanceToMove moves the file pointer forward in the file, and a negative value moves the file pointer backward. Note that you cannot use a negative value to move back past beyond the beginning of a file. 
    lpDistanceToMoveHigh 
    Not supported; must be NULL or point to a value of zero. 
    dwMoveMethod 
    [in] Starting point for the file pointer move. The following table shows possible values for this parameter. 
    Value Description 
    FILE_BEGIN Indicates that the starting point is zero or the beginning of the file. 
    FILE_CURRENT Indicates that the starting point is the current value of the file pointer. 
    FILE_END Indicates that the starting point is the current EOF position.