请问怎么用CreateFile WriteFile创建与写入一个文件 (Win32下)比如我要在C盘创建一个hello.txt写入的内容是"hello!"请问具体写法,急呀,,,

解决方案 »

  1.   

    #include <windows.h>
    #include <stdio.h>#define BUFSIZE 4096int main()
    {
        HANDLE hFile;
        HANDLE hTempFile; 
        DWORD  dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
        char szTempName[MAX_PATH];
        char buffer[BUFSIZE]; 
        char lpPathBuffer[BUFSIZE];
     
        // Open the existing file. 
     
        hFile = CreateFile("original.txt",  // file name 
            GENERIC_READ,                   // open for reading 
            0,                              // do not share 
            NULL,                           // default security 
            OPEN_EXISTING,                  // existing file only 
            FILE_ATTRIBUTE_NORMAL,          // normal file 
            NULL);                          // no template 
        if (hFile == INVALID_HANDLE_VALUE) 
        { 
            printf("Could not open file.");
            return 0;
        } 
     
        // Get the temp path    GetTempPath(dwBufSize,   // length of the buffer
             lpPathBuffer);      // buffer for path     // Create a temporary file. 
        
        GetTempFileName(lpPathBuffer, // directory for temp files 
            "NEW",                    // temp file name prefix 
            0,                        // create unique name 
            szTempName);              // buffer for name     hTempFile = CreateFile((LPTSTR) szTempName,  // file name 
            GENERIC_READ | GENERIC_WRITE, // open for read/write 
            0,                            // do not share 
            NULL,                         // default security 
            CREATE_ALWAYS,                // overwrite existing file
            FILE_ATTRIBUTE_NORMAL,        // normal file 
            NULL);                        // no template     if (hTempFile == INVALID_HANDLE_VALUE) 
        { 
            printf("Could not create temporary file."); 
            return 0;
        } 
     
        // Read 4K blocks to the buffer. 
        // Change all characters in the buffer to upper case. 
        // Write the buffer to the temporary file. 
     
        do 
        {
            if (ReadFile(hFile, buffer, 4096, 
                &dwBytesRead, NULL)) 
            { 
                CharUpperBuff(buffer, dwBytesRead); 
      
                WriteFile(hTempFile, buffer, dwBytesRead, 
                    &dwBytesWritten, NULL); 
            } 
        } while (dwBytesRead == BUFSIZE); 
     
        // Close both files. 
     
        CloseHandle(hFile); 
        CloseHandle(hTempFile); 
     
        // Move the temporary file to the new text file.
     
        if (!MoveFileEx(szTempName, 
                        "allcaps.txt", 
                        MOVEFILE_REPLACE_EXISTING)) 
        { 
            printf("Could not move temp file.");
            return 0;
        }
    }
    msdn 里面的例子
      

  2.   

    看不懂,暈哦...我就想这样而已.请问怎么用CreateFile WriteFile创建与写入一个文件 (Win32下)比如我要在C盘创建一个hello.txt写入的内容是"hello!"不想去读文件再写入,帮我修改修改吧,分不够,我再加,求你了...
      

  3.   

    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>#define BUFSIZE 4096
    int main(int argc, char* argv[])
    {
        HANDLE hTempFile; 
        DWORD  dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
        char szTempName[MAX_PATH];
        char buffer[BUFSIZE]; 
        char lpPathBuffer[BUFSIZE];
     
        // Open the existing file. 
     
        hTempFile = CreateFile("c:\\hello.txt" , // file name 
            GENERIC_READ | GENERIC_WRITE, // open for read/write 
            0,                            // do not share 
            NULL,                         // default security 
            CREATE_ALWAYS,                // overwrite existing file
            FILE_ATTRIBUTE_NORMAL,        // normal file 
            NULL);                       // no template   
     
    DWORD dwsize=6;
    memcpy(lpPathBuffer,"hello",dwsize);
    WriteFile(hTempFile, lpPathBuffer, 6, 
                    &dwsize, NULL);      CloseHandle(hTempFile); 
     
        // Move the temporary file to the new text file.
     
    return 0;
    }好了,不过写的不规范,自己改改,
    给分吧