views:

71

answers:

1

hi I have to write a windows api code that encrypts a file by adding three to each character.

so I wrote this now its not doing anything ... where i go wronge

    #include "stdafx.h"
    #include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE filein,fileout;

    filein=CreateFile
    (L"d:\\test.txt",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

    fileout=CreateFile
    (L"d:\\test.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

    DWORD really; //later this will be used to store how many bytes I succeed to read
    do
    {
        BYTE x[1024];  //the buffer the thing Im using to read in
        ReadFile(filein,x,1024,&really,NULL);

        for(int i=0 ; i<really ; i++)
        {
            x[i]= (x[i]+3) % 256; 
        }

        DWORD really2;
        WriteFile(fileout,x,really,&really2,NULL);

    }while(really==1024);

    CloseHandle(filein);
    CloseHandle(fileout);

    return 0;
}

and if Im right how can i know its ok

+2  A: 

First, you can't overwrite a file that's in use. You will need to use different pathnames for your input and output, and then rename files at the end.

Joe
Or he could read the entire file into memory, then overwrite it. There are *a lot* of disadvantages in using that method, of course.
Brian
@Joe Thanx that work@Brian can you give me a hint on how to do so ?
Kristian