Question

#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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top