tags:

views:

67

answers:

2
#include <Windows.h> 
#include <stdio.h> 

int count = 0; 
FILE* pFile = 0; 
long Size = 0; 

void *memfrob(void * s, size_t n) 
{ 
    char *p = (char *) s; 

    while (n-- > 0) 
        *p++ ^= 42; 
    return s; 
} 

int main() 
{ 
    fopen_s(&pFile, "***", "r+"); 
    fseek(pFile, 0, SEEK_END); 
    Size = ftell(pFile); 
    char *buffer = (char*)malloc(Size); 
    memset(buffer, 0, Size); 
    fread(buffer, Size, 1, pFile); 
    fclose(pFile); 
    memfrob(buffer, Size); 
    fopen_s(&pFile, "***", "w+"); 
    fwrite(buffer, Size, 1, pFile); 
    fclose(pFile); 
}

Hi, fread isn't reading anything from file to buffer and I can't figure out why. Could someone give me a hint or a push in the right direction?

+7  A: 

You need to seek back to the beginning of the file before you fread.

jcopenha
There is also no need to close and reopen the file before writing; the `"r+"` mode means it is open for read and write. You would, however, need to seek to the start once more before doing the write.
Jonathan Leffler
+3  A: 

You did a fseek to the end of the file and didn't fseek back before you did the fread.

Paul Tomblin