Overwrite data in a file

This is a discussion on Overwrite data in a file within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm trying to overwrite a number of bytes in a file. Using the standard C libraries it would be ...

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

    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,259
    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,259
    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, 08:35 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  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, 03:21 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21