2010-09-19 130 views
4
#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); 
} 

嗨,fread没有读取任何文件到缓冲区,我找不出原因。有人能给我一个暗示或推动正确的方向吗?fread()没有写入缓冲区

+0

检查文件操作中的错误返回代码 - 您不知道它们中的哪些失败。 – 2010-09-19 00:25:20

回答

11

在fread之前,您需要回溯到文件的开头。

+0

写作前无需关闭并重新打开文件; ''r +''模式意味着它是开放的读取和写入。但是,在写作之前,你需要再次寻求开始。 – 2010-09-19 00:27:52

4

你做了一个fseek到文件末尾,在你做fread之前没有回过头。