Win32 API를 이용한 파일 입출력 예시.
 
#include <windows.h>
#include <stdio.h>

#define MAX_LINE 1024 

int main(int argc, char **argv)
{
    char buf[MAX_LINE];
    HANDLE fh_src, fh_desc;
    int r_size, w_size; 

    if (argc != 3)
    {
        printf("Usge : %s [source file] [dest file]\n", argv[0]);
        return 0;
    } 

    fh_src= CreateFile( argv[1], GENERIC_READ, 0, NULL, 
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
   
   if(fh_src == INVALID_HANDLE_VALUE)
    {
        printf("File Open Error %d  \n", GetLastError() );
        return 1;
    } 

    fh_desc = CreateFile(argv[2], GENERIC_WRITE, 0, NULL,
        CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); 

    if (fh_desc == INVALID_HANDLE_VALUE)
    {
        if (GetLastError() == ERROR_FILE_EXISTS) 
            printf("Dest File Already exists\n");
        else 
            printf("Dest File Open Error\n");
        return 1;
    } 

    while(1)
    {
        memset(buf, 0x00, MAX_LINE);
        ReadFile(fh_src, buf, MAX_LINE-1, (PTDWORD)&r_size, NULL); 

        if(r_size <= 0)
            break; 

        WriteFile(fh_desc, buf, r_size, (PTDWORD)&w_size, NULL); 
    }
    return 0;



Posted by 혀나미
,