手记

从一个文件中读取数据到内存,然后再把内存中的数据写入另外一个文件


//从一个文件中读取数据到内存,然后再把内存中的数据写入另外一个文件

#include "stdafx.h"

#include "stdlib.h"

int main(int argc, char* argv[])

{

FILE* fp;

FILE* fp2;

fp = fopen("C:/notepad.exe","rb");

fp2 = fopen("C:/aa.exe","wb");

fseek(fp,0,SEEK_END);

int size = ftell(fp);

fseek(fp,0,SEEK_SET);

unsigned char* buffer = (unsigned char*)malloc(size);

fread(buffer,size,1,fp);

fwrite(buffer, size, 1, fp2);

free(buffer);

fclose(fp);

fclose(fp2);

return 0;

}

//fopen 返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno 中。

//fseek int fseek(FILE *stream, long offset, int fromwhere);函数设置文件指针stream的位置

//ftell 函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。

//fclose 使用fclose()函数就可以把缓冲区内最后剩余的数据输出到内核缓冲区,并释放文件指针和有关的缓冲区。

©著作权归作者所有:来自51CTO博客作者紫气东来33的原创作品,如需转载,请注明出处,否则将追究法律责任


0人推荐
随时随地看视频
慕课网APP