/*
COPYRIGHT H. HERNAN MORALDO aka DoctorK
asciimail@yahoo.com
Do with this file what you like, but I'm not responsible
if this program eats your cat or something.
Anyway, if you like you can give me credit, or say to all
your friendd that I changed your life with my tutorial.
*/
#include <windows.h>

//open a file, and set the first 99 characters as
//uppercase using normal file handling functions
void fileExample1(){
    char data[100];
    DWORD dwUseless;
    char* dataPointer=data;
    DWORD maxLen;

    data[99]=0;

    HANDLE hFile=CreateFile(
        "hithere.txt",//name of the file
        GENERIC_READ | GENERIC_WRITE,//desired access
        FILE_SHARE_READ,//share mode
        NULL,//security attributes
        OPEN_ALWAYS,//creation disposition
        FILE_ATTRIBUTE_NORMAL,//flags and attr
        NULL);//template file

    //that's to don't resize the file
    maxLen=GetFileSize(hFile,NULL);
    maxLen=(maxLen>99)?99:maxLen;

    ReadFile(hFile,data,maxLen,&dwUseless,NULL);

    CharUpperBuff(data, maxLen);   
 
    SetFilePointer(hFile,0,NULL,FILE_BEGIN);

    WriteFile(hFile,data,maxLen,&dwUseless,NULL);

    CloseHandle(hFile);
}

//open a file, and set the first 99 characters as
//uppercase using file mapping
void fileExample2(){
    char* dataPointer;
    DWORD maxLen;

    HANDLE hFile=CreateFile(
        "hithere.txt",//name of the file
        GENERIC_READ | GENERIC_WRITE,//desired access
        FILE_SHARE_READ,//share mode
        NULL,//security attributes
        OPEN_ALWAYS,//creation disposition
        FILE_ATTRIBUTE_NORMAL,//flags and attr
        NULL);//template file

    //that's to avoid Windows killing the program
    maxLen=GetFileSize(hFile,NULL);
    maxLen=(maxLen>99)?99:maxLen;

    HANDLE hFM=CreateFileMapping(
        hFile,//handle
        NULL,//security
        PAGE_READWRITE,//flProtect
        0,0,//max size
        NULL);//name

    dataPointer=(char*)MapViewOfFile(
        hFM,FILE_MAP_ALL_ACCESS,0,0,0);

    CharUpperBuff(dataPointer,
        maxLen//don't pass the limit!
        );   

    UnmapViewOfFile(dataPointer);

 
    CloseHandle(hFM);
    CloseHandle(hFile);
}