Thread: Overwrite data in a file

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Angry Overwrite data in a file

    Hello,

    I'm trying to overwrite a number of bytes in a file. Using the standard C libraries it would be a simple case of:-
    Code:
    FILE *fp;
    unsigned char szData[] = "Hello";
    
    fp = fopen("file", "w");
    fseek(fp, 69, SEEK_SET);
    fwrite(szData, strlen(szData), 1, fp);
    fclose(fp);
    But using the Win32 API does not seem to be as simple. Nice one M$.

    The best I can come up with is:-
    Code:
    HANDLE hFile;
    unsigned char szData[]= "Hello";
    DWORD dwNumBytesWritten;
    
    hFile = CreateFile("file", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    SetFilePointer(hFile, 69, NULL, FILE_BEGIN);
    WriteFile(hFile, szData, lstrlen(szData), &dwNumBytesWritten, NULL);
    CloseHandle(hFile);
    Sure, it writes the data, but appends it as opposed to overwriting it, increasing the size of the file. Is there a flag or sommat I'm missing?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is file-mapping. Map the file, change the bytes, and write the file back to disk.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    There's nothing wrong with using the C libraries or the C++ libraries for file I/O Just do like you posted with the FILE pointer or use the fstream. I'm not sure how I'd do it using only the Win32 API
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Nice one kuphryn, I've got the file mapped now, but an M$ programmer left an int 3 (User breakpoint) in NTDLL, which is preventing me from using lstrcmpi with the mapped file. Apparently it's because RtlValidateHeap has an invalid address specified. I hate it when they do that

    Can I use string comparison functions with mapped file memory, or should I copy any strings I wanna look at out first?

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Yes, you can use string comparison tools on mapped files. Cast the bytes into char or TCHAR and apply the string tools. If that still does not work, consider mem tools such as memcmp.

    Kuphryn

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Whoops, my mistake. Was freeing a declared character array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM