从流(二进制文件)中读取数据块
即从流中读取 count 个元素的数组,每个元素的大小为size,并将它们存储在 ptr 指定的内存块中。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* pFile = NULL;
long lSize;
char* buffer;
size_t result;
pFile = fopen("myfile.bin", "rb");
if (pFile == NULL)
{
fputs("File error", stderr);
exit(1);
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = (char*)malloc(sizeof(char) * lSize);
if (buffer == NULL)
{
fputs("Memory error", stderr);
exit(2);
}
// copy the file into the buffer:
result = fread(buffer, 1, lSize, pFile);
if (result != lSize)
{
fputs("Reading error", stderr);
exit(3);
}
fclose(pFile);
free(buffer);
return 0;
}
fwrite和fread的理解差不多,这里就不多做阐述啦!
#include <stdio.h>
int main()
{
FILE* pFile = NULL;
char buffer[] = { 'x' , 'y' , 'z' };
pFile = fopen("myfile.bin", "wb");
fwrite(buffer, sizeof(char), sizeof(buffer), pFile);
fclose(pFile);
return 0;
}