- fread函数:
- 功能:从文件中读取
nmemb 项数据
,其中每项
数据size个字节
,并存放到指针ptr
; - 具体内容:
#include
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- fwrite函数:
- 功能:向文件中写入
nmemb 项数据
,其中每项数据size个字节
; - 具体内容:
#include
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
#include
int main(int argc, const char *argv[]){
if(3 != argc){
printf("Usage : %s src_file dest_file\n",argv[0]);
return -1;
}
FILE *fd1 = fopen(argv[1],"r");
if(NULL == fd1)
{
perror("fopen error");
return -1;
}
FILE *fd2 = fopen(argv[2],"w");
if(NULL == fd2)
{
perror("fopen error");
return -1;
}
int ret = 0;
char buff[128] = {0};
while(!(feof(fd1) || ferror(fd1))){
ret = fread(buff,1,sizeof(buff),fd1);
fwrite(buff,1,ret,fd2);
}
fclose(fd1);
fclose(fd2);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46