文件操作


#include

void test1()

{

/*

w:(1)文件存在,将文件清空,再进行写,(2)文件不存在,新建文件

r:(1)文件存在,正常打开(2)文件不存在,打开失败

*/

/**

*  1.打开文件

*  @param restrict#> 文件路径 description#>

*  @param restrict#> 文件打开的方式 description#>

*  @return

*/

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

if(fp==NULL)

{

printf("打开失败\n");

return ;

}

//2.读或写操作

/**  *  fputc:是将一个字符写入到文件里,

*/

char ch='b';

int res=fputc(ch, fp);

printf("res=%d\n",res);


//3.关闭文件

fclose(fp);

}

void test2()

{  //1.打开文件

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

if(NULL==fp)

{

perror("fopen failed");

return ;

}

//2.对文件操作:

fgetc:意为从文件中读取一个字符,读取一个字节后,光标位置后移一个字节。

int ch=fgetc(fp);

printf("第一次读ch=%c\n",ch);

ch=fgetc(fp);

printf("第二次读ch=%d\n",ch);

printf("EOF=%d\n",EOF);

//3.关闭文件    fclose(fp);

}


void test3()

{

/*

拷贝文件4.txt里面的内容到3.txt里面。用fputc,fgetc实现


*/

}

void test4()

{

//1.fprintf:fprintf( )会根据参数format 字符串来转换并格式化数据, 然后将结果输出到参数stream 指定的文件中, 直到出现字符串结束('\0')为止。

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

if(fp==NULL)

{

perror("fopen failed:");

return ;

}

//2.

fprintf(fp, "a=%d;;;;;b=%d\n",10,5);

//3.

fclose(fp);

}


void test5()

{

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

if(fp==NULL)

{

perror("fopen failed:");

return ;

}    //2.

int a,b;

fscanf(fp, "a=%d;;;;;b=%d\n",&a,&b);//fscanf:其功能为根据数据格式(format)从输入流(stream)中写入数据(argument);与fgets的差别在于:fscanf遇到空格和换行时结束,注意空格时也结束,fgets遇到空格不结束。

printf("a=%d,b=%d\n",a,b);


//3.

fclose(fp);

}

void test6(){

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "a+");

if(fp==NULL)

{

perror("fopen failed:");

return ;

}

//unsigned char *_p

/*    ftell:告诉你文件指针相对偏移量的位置

*/

long position=ftell(fp);

printf("position=%ld\n",position);

//2.

fputc('a', fp);

position=ftell(fp);

printf("position=%ld\n",position);

fputc('b', fp);

position=ftell(fp);

printf("position=%ld\n",position);

fclose(fp);

}

void test7(){

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

if(fp==NULL)

{

perror("fopen failed:");

return ;

}

//2.

//fputs:将字符串写进文件里。

fputs("wo men ban hen niu bi\nxiao wai sheng hen cai\n", fp);

//3.    fclose(fp);


}

void test8()

{

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

//2.

/*

1>将内容保存指定的变量里,    2>指定多大个字节接收,

3>指定文件

*/

/*

fgets():读取一行,碰到\n,表示读取完成

*/

char string[50];

fgets(string, 50, fp);

printf("string=%s",string);

fgets(string, 50, fp);

printf("string=%s",string);    //3.

fclose(fp);

}


void test9()

{

int arr[5]={0,2,3,4,5};

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

/*

参数解释

1>存储变量的地址

2>变量多大

3>存储这种类型变量的个数

4>存储到哪个文件

fwrite:

size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);

注意:这个函数以二进制形式对文件进行操作,不局限于文本文件

返回值:返回实际写入的数据块数目

(1)buffer:是一个指针,对fwrite来说,是要获取数据的地址;

(2)size:要写入内容的单字节数;

(3)count:要进行写入size字节的数据项的个数;

(4)stream:目标文件指针

(5)返回实际写入的数据项个数count。

说明:写入到文件的哪里? 这个与文件的打开模式有关,如果是w+,则是从file pointer指向的地址开始写,替换掉之后的内容,文件的长度可以不变,stream的位置移动count个数;如果是a+,则从文件的末尾开始添加,文件长度加大。

fseek对此函数有作用,但是fwrite[1]函数写到用户空间缓冲区,并未同步到文件中,所以修改后要将内存与文件同步可以用fflush(FILE *fp)函数同步。


*/    fwrite(arr, sizeof(int), 5, fp);

fclose(fp);

}


void test10()

{

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

//2.    /*

参数解释

1>将读到的内容存到哪个变量里,指定变量的地址

2>变量多大

3>读取这种类型变量的个数

4>读取哪个文件

fread:

fread是一个函数。从一个文件流中读数据,最多读取count个项,每个项size个字节,如果调用成功返回实际读取到的项个数(小于或等于count),如果不成功或读到文件末尾返回 0。

*/

int brr[5];

fread(brr, sizeof(int), 5, fp);

int i=0;

for (; i<5; i++)

{

printf("brr[%d]=%d\n",i,brr[i]);

}

fclose(fp);


}

typedef struct Student

{

char sex;

int age;

}Student;

void test11()

{

Student stu={'x',20};

//1.

FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

//2.

fwrite(&stu, sizeof(stu), 1, fp);

//3.

fclose(fp);

}

void test12()

{

Student stu;

//1.    FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

//2.

fread(&stu, sizeof(stu), 1, fp);

printf("age=%d,sex=%c\n",stu.age,stu.sex);

//3.

fclose(fp);

}

void test13()

{

Student stu[3]={{'x',20},{'m',30},{'b',40}};

//1.    FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");

//2.

int i=0;

for (; i<3; i++)

{

fwrite(&stu[i], sizeof(Student), 1, fp);

}

//3.

fclose(fp);

}

void test14()

{

Student stu[3];

//1.    FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");

//2.

fread(stu, sizeof(stu), 1, fp);

int i=0;

for (; i<3; i++)

{

printf("stu[%d]的age=%d,sex=%c\n",i,stu[i].age,stu[i].sex);

}

//3.

fclose(fp);

}

int main()

{

test14();

return 0;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容