BOOL WriteFile(
  HANDLE hFile,                    // handle to file to write to
  LPCVOID lpBuffer,                // pointer to data to write to file
  DWORD nNumberOfBytesToWrite,     // number of bytes to write
  LPDWORD lpNumberOfBytesWritten,  // pointer to number of bytes written
  LPOVERLAPPED lpOverlapped        // pointer to structure for overlapped I/O
);
请问第二个参数该怎么用??我想把一缓冲器(INbuffer)中的数据写入一文件,但不知第二个参数是什么意思,请问我该怎么把INbuffer中数据写入文件??

解决方案 »

  1.   

    实现文件拷贝:// CopyEx.cpp : Defines the entry point for the console application.
    //#include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>     /* For _MAX_PATH definition */
    #include <conio.h>static BOOL MyCopyFile(HANDLE hSource,HANDLE hDestination);int main(int argc, char* argv[])
    {
        HANDLE hstdOut,hstdIn;
        HANDLE hSource,hDestination;
        DWORD dwWritten;
        char szSourceFile[_MAX_PATH],szDestFile[_MAX_PATH];
        char szMsgBuf[255];
        WIN32_FIND_DATA FindData;
        HANDLE hFind;    hstdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        hstdIn  = GetStdHandle(STD_INPUT_HANDLE);
        if(hstdOut==INVALID_HANDLE_VALUE || hstdIn==INVALID_HANDLE_VALUE)
        {
            return -1;
        }    if (argc !=3)
        {
            sprintf(szMsgBuf,"\nParameter Error!\nPlease use as: CopyEx SourceFile DestinationFile\n");
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            return -1;
        }
        
        strcpy(szSourceFile,argv[1]);
        strcpy(szDestFile,argv[2]);    /*查找源文件是否存在*/
        memset(&FindData,0X00,sizeof(WIN32_FIND_DATA));
        hFind = FindFirstFile(szSourceFile,&FindData);
        if(hFind == INVALID_HANDLE_VALUE)
        {
            sprintf(szMsgBuf,"\nFile %s Isn't Exist!\n",szSourceFile);
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            return -1;
        }
        FindClose(hFind);    /*查找目标文件是否存在*/
        memset(&FindData,0X00,sizeof(WIN32_FIND_DATA));
        hFind = FindFirstFile(szDestFile,&FindData);
        if(hFind != INVALID_HANDLE_VALUE)
        {
            DWORD dwRead=10;
            char Key;        sprintf(szMsgBuf,"\nFile %s Exist!Relpace It?(Y/N)",szDestFile);
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            //ReadFile(hstdIn,szRead,dwRead,&dwRead,NULL);        Key=_getch();
            Key=toupper(Key);
            if (Key!='Y')
            {
                sprintf(szMsgBuf,"\nTerminate Operation\n");
                WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
                return 0;
            }
        }
        FindClose(hFind);    hSource = CreateFile(
            szSourceFile,
            GENERIC_READ,
            0,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);    if (hSource==INVALID_HANDLE_VALUE)
        {
            sprintf(szMsgBuf,"\nOpen %s File Failed!\n",szSourceFile);
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            return -1;
        }
        
        hDestination = CreateFile(
            szDestFile,
            GENERIC_WRITE,
            0,
            NULL,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
        if (hSource==INVALID_HANDLE_VALUE)
        {
            sprintf(szMsgBuf,"\nOpen %s File Failed!\n",szDestFile);
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            CloseHandle(hSource);
            return -1;
        }    sprintf(szMsgBuf,"\nStart Copy File %s To File %s \n    ...... ......\n",szSourceFile,szDestFile);
        WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
        if(!MyCopyFile(hSource,hDestination))    
        {
            sprintf(szMsgBuf,"\nCopy File Failed!\n");
            WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);
            CloseHandle(hSource);
            CloseHandle(hDestination);
            return -1;
        }
        sprintf(szMsgBuf,"\nCopy File Succeeded!\n");
        WriteFile(hstdOut,szMsgBuf,lstrlen(szMsgBuf),&dwWritten,NULL);    CloseHandle(hSource);
        CloseHandle(hDestination);
        return 0;
    }static BOOL MyCopyFile(HANDLE hSource,HANDLE hDestination)
    {
        DWORD dwReadCount;
        DWORD dwFileSize;
        CHAR szReadBuf[1000];    dwFileSize = GetFileSize(hSource,NULL);
        dwReadCount = 0;
        while(dwFileSize>dwReadCount)
        {
            DWORD dwWriteSize=0;
            DWORD dwReadSize=1000;  //每次读1000个字节
             
            if(!ReadFile(hSource,szReadBuf,dwReadSize,&dwReadSize,NULL))
            {
                return FALSE;
            } 
            
            dwWriteSize = dwReadSize;
            if(!WriteFile(hDestination,szReadBuf,dwWriteSize,&dwWriteSize,NULL))
            {
                return FALSE;
            } 
            else
            {
                dwReadCount += dwReadSize;
            }
        }
        return TRUE;
    }
      

  2.   

    请问第二个参数该怎么用??我想把一缓冲器(INbuffer)中的数据写入一文件,但不知第二个参数是什么意思,请问我该怎么把INbuffer中数据写入文件??
    -------------------------------------------------------
    楼主,你把缓冲器中得数据指针作为第二个参数传入
    至于第二个参数得含义,他得注释写得很清楚了
    pointer to data to write to file
    即:要写入文件得数据指针
      

  3.   

    BOOL WriteFile(
      HANDLE hFile,                    // 文件的句柄,用OpenFile产生
      LPCVOID lpBuffer,                // 要写入的数据的内存,就是你的INbuffer
      DWORD nNumberOfBytesToWrite,     // INbuffer的长度
      LPDWORD lpNumberOfBytesWritten,  // 实际写入的长度
      LPOVERLAPPED lpOverlapped        // pointer to structure for overlapped I/O
    );
      

  4.   

    我是楼主,帮看看WriteFile(m_hFile, iNbuffer, 2, &iWrite, NULL);
    这样写出错啊,错误信息是:
    error C2664: 'WriteFile' : cannot convert parameter 2 from 'class std::list<char,class std::allocator<char> >' to 'const void *'什么原因啊??谢谢!
      

  5.   

    iNbuffer 应该定义成缓冲区地址啊
      

  6.   

    错误提示很明显了那个参数应该是一个指针你却传入一个实型变量当然不行用_itoa()方法把这个变量转化为array吧
      

  7.   

    error C2664: 'WriteFile' : cannot convert parameter 2 from 'class std::list<char,class std::allocator<char> >' to 'const void *'
    ---------------
    类型错误
    转化成char数组试试