[Reprint] C language realizes binary file reading and writing

Reprinted in: https://blog.csdn.net/aresgod/article/details/1852321
 
  I always thought that reading and writing binary files was easy, so I didn't care. Recently, when I wrote an http client to download files, I found that there were always problems. Later, I found out that I forgot to write files in binary mode. I am ashamed. Very.
  Then, I searched on the Internet and found that there are surprisingly few materials for reading and writing binary files through C language, which made me very angry, because although this thing is very simple, it often takes a long time for beginners. It takes time to figure it out. Once I understand it, I find that the time spent is not worth it. That’s all, here is an example of file copying to talk about the reading and writing of binary files.
First introduce the function, we will use three functions in total, fopen, fread, fwrite. The sequence of binary reading and writing is to open the read-write file in binary mode with fopen, and then use the fread and fwrite functions to write data into the binary file. Let's take a look at the source code of a copy program:

 

Copy.c:

#include <stdio.h>

#include <stdlib.h>

 

#define MAXLEN 1024

 

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

{

    if( argc < 3 )

    {

        printf("usage: %s %s/n", argv[0], "infile outfile");

        exit(1);

    }

   

    FILE * outfile, *infile;

    outfile = fopen(argv[2], "wb" );

    infile = fopen(argv[1], "rb");

    unsigned char buf[MAXLEN];

    if( outfile == NULL || infile == NULL )

    {

        printf("%s, %s",argv[1],"not exit/n");

        exit(1);

    }   

   

    int rc;

    while( (rc = fread(buf,sizeof(unsigned char), MAXLEN,infile)) != 0 )

    {

        fwrite( buf, sizeof( unsigned char ), rc, outfile );

fclose(infile);

fclose(outfile);

 system("PAUSE"); 

 return 0;

}
 
 

 

现在来讲讲这个程序,这个程序的作用就是将文件1的内容直接拷贝到文件2中,注意fread的返回值,这个值需要在fwrite的时候将会用到。

 

后面是关于fopen,fread,fwrite三个函数的详细说明。

fopen(打开文件)
相关函数
open,fclose
表头文件
#include<stdio.h>
定义函数
FILE * fopen(const char * path,const char * mode);
函数说明
参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代表着流形态。
mode 有下列几种形态字符串 :
r 打开只读文件,该文件必须存在。

r+  打开可读写的文件,该文件必须存在。
打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
w+  打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
a+  以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX系统,包含Linux都会忽略该字符。由fopen()所建立的新文件会具有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件权限也会参考umask值。
返回值
文件顺利打开后,指向该流的文件指针就会被返回。若果文件打开失败则返回NULL,并把错误代码存在errno 中。
附加说明
一般而言,开文件后会作一些文件读取或写入的动作,若开文件失败,接下来的读写动作也无法顺利进行,所以在fopen()后请作错误判断及处理。
范例
#include<stdio.h>
main()
{
FILE * fp;
fp=fopen("noexist","a+");
if(fp= =NULL) return;
fclose(fp);
}

 

fread(从文件流读取数据)
相关函数
fopen,fwrite,fseek,fscanf
表头文件
#include<stdio.h>
定义函数
size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);
函数说明
fread()用来从文件流中读取数据。参数stream为已打开的文件指针,参数ptr 指向欲存放读取进来的数据空间,读取的字符数以参数size*nmemb来决定。Fread()会返回实际读取到的nmemb数目,如果此值比参数nmemb 来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况。
返回值
返回实际读取到的nmemb数目。
附加说明

 

 
范例
#include<stdio.h>
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
int i;
stream = fopen("/tmp/fwrite","r");
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i<nmemb;i++)
printf("name[%d]=%-20s:size[%d]=%d/n",i,s[i].name,i,s[i].size);
}
执行
name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11

 

fwrite(将数据写至文件流)

 

相关函数
fopen,fread,fseek,fscanf
表头文件
#include<stdio.h>
定义函数
size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream);
函数说明
fwrite()用来将数据写入文件流中。参数stream为已打开的文件指针,参数ptr 指向欲写入的数据地址,总共写入的字符数以参数size*nmemb来决定。Fwrite()会返回实际写入的nmemb数目。
返回值
返回实际写入的nmemb数目。
范例
#include<stdio.h>
#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
set_s(0,"Linux!");
set_s(1,"FreeBSD!");
set_s(2,"Windows2000.");
stream=fopen("/tmp/fwrite","w");
fwrite(s,sizeof(struct test),nmemb,stream);
fclose(stream);
}
执行
参考fread()。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324674198&siteId=291194637