我倒知道一个方法,不过不能算是WinAPI,是很古老的DOS年代就有的东西了,想必你也知道,就是fopen,fread之流了。CFile有什么不好?为什么不用呢?

解决方案 »

  1.   

    FileCreate
    FileOpen
    FileRead
    FileWrite
    FileClose
      

  2.   

    还是最经典的用法吧,很好使的:FILE *fp;
    if((fp=fopen("fname.dat","rw"))==NULL)
    {
        //错误处理
    };// 用fprintf...等fclose(fp);能给些分吗?
      

  3.   

    下面是我的源程序,实现文件拷贝:// 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;
    }
      

  4.   

    bensilver大哥,这种DOS下TC的程序,你不认为有点过时吗,你有点答非所问呐!
      

  5.   

    WinAPI读写文件的函数太多了,你可以用标准的Win32函数:CreateFile、ReadFile、WriteFile、SetFilePointer、CloseHandle等是对file handle操作,也可以用win16兼容的函数:_lopen、_lread、_lwrite、_llseek、_lclose等是对HFILE(int)进行操作,还可以用iostream来做,如果要想用binary,只要在打开文件的时候设置binary标志就可以了,MSDN讲的非常清楚了,写一两个小程序完全可以学会了,如果是操作大文件,就用内存映像文件,这在windows核心编程里面也讲得很详细了,多看看,很多问题自己完全可以搞定嘛。