当前位置: 首页 > news >正文

免费网页代理ip地址网站快优吧seo优化

免费网页代理ip地址网站,快优吧seo优化,长沙做无痛肠镜东大医院l网站,网站备案注销申请表本章重点: 什么是文件 文件名 文件类型 文件缓冲区 文件指针 文件的打开和关闭文件的顺序读写文件的随机读写文件结束的判定 1. 什么是文件 磁盘上的文件是文件。 但是在程序设计中,我们一般谈的文件有两种:程序文件、数据文件 1.1 程序文件…

本章重点:

什么是文件
文件名
文件类型
文件缓冲区
文件指针
文件的打开和关闭文件的顺序读写文件的随机读写文件结束的判定

 1.  什么是文件

磁盘上的文件是文件。
但是在程序设计中,我们一般谈的文件有两种:程序文件、数据文件

 1.1 程序文件

包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。

 1.2 数据文件

文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。

 1.3 文件名

一个文件要有一个唯一的文件标识,以便用户识别和引用。文件名包含3部分:文件路径+文件名主干+文件后缀
例如: c:\code\test.txt
为了方便起见,文件标识常被称为文件名。

 2. 文件的打开和关闭

 2.1 文件指针

缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。

每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE. 例如,VS2008编译环境提供的 stdio.h头文件中有以下的文件类型申明:

struct _iobuf {char *_ptr;int _cnt;char *_base;int _flag;int _file;int _charbuf;int _bufsiz;char *_tmpfname;
};
typedef struct _iobuf FILE;

不同的C编译器的FILE类型包含的内容不完全相同,但是大同小异。

每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息,使用者不必关

心细节。

一般都是通过一个FILE的指针来维护这个FILE结构的变量,这样使用起来更加方便。

下面我们可以创建一个FILE*的指针变量:

FILE* pf;//文件指针变量

定义pf是一个指向FILE类型数据的指针变量。可以使pf指向某个文件的文件信息区(是一个结构体变量)。通过该文 件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它关联的文件。 

2.2 文件的打开与关闭

int main()
{//相对路径//绝对路径///Users/fan/Documents/c_study/c_test26/data.txt//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件//关闭文件fclose(pf);pf = NULL;return 0;
}

 3. 文件的顺序读写

 3.1 顺序读写函数介绍

 

 

int main()
{//相对路径//绝对路径///Users/fan/Documents/c_study/c_test26/data.txt//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","w");if (pf == NULL){perror("fopen");return 1;}//读文件//写文件// fputc('a',pf);// fputc('b',pf);// fputc('c',pf);int i = 0;for (i = 0; i < 26; i++){fputc('a'+i,pf);}//关闭文件fclose(pf);pf = NULL;return 0;
}
int main()
{//相对路径//绝对路径///Users/fan/Documents/c_study/c_test26/data.txt//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件int ch = fgetc(pf);printf("%c\n",ch);  //ach = fgetc(pf);printf("%c\n",ch); //bch = fgetc(pf);printf("%c\n",ch); //cch = fgetc(pf);printf("%c\n",ch);//d//写文件// fputc('a',pf);// fputc('b',pf);// fputc('c',pf);// int i = 0;// for (i = 0; i < 26; i++)// {//     fputc('a'+i,pf);// }//关闭文件fclose(pf);pf = NULL;return 0;
}
int main()
{//相对路径//绝对路径///Users/fan/Documents/c_study/c_test26/data.txt//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件// int ch = fgetc(pf);// printf("%c\n",ch);  //a// ch = fgetc(pf);// printf("%c\n",ch); //b// ch = fgetc(pf);// printf("%c\n",ch); //c// ch = fgetc(pf);// printf("%c\n",ch);//d//写文件// fputc('a',pf);// fputc('b',pf);// fputc('c',pf);// int i = 0;// for (i = 0; i < 26; i++)// {//     fputc('a'+i,pf);// }// fputs("hello fan\n",pf);// fputs("hello fanfan\n",pf);//读文件 - 读一行char arr[10] = {0};fgets(arr,15,pf);printf("%s\n",arr);fgets(arr,15,pf);printf("%s\n",arr);//关闭文件fclose(pf);pf = NULL;return 0;
}
struct S
{int a;float s;
};int main()
{//相对路径//绝对路径///Users/fan/Documents/c_study/c_test26/data.txt//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//写文件//struct S s = {100,3.14f};//fprintf(pf,"%d %f",s.a,s.s);struct S s = {0};fscanf(pf,"%d %f",&(s.a),&(s.s));printf("%d %f",s.a,s.s);//关闭文件fclose(pf);pf = NULL;return 0;
}
struct S
{int a;float s;char str[10];
};int main()
{struct S s = {99, 6.18f, "bit"};//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","wb");if (pf == NULL){perror("fopen");return 1;}//写文件fwrite(&s,sizeof(struct S),1,pf);//关闭文件fclose(pf);pf = NULL;return 0;
}
int main()
{struct S s = {0};//打开文件FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","rb");if (pf == NULL){perror("fopen");return 1;}//读文件fread(&s,sizeof(struct S),1,pf);printf("%d %f %s\n",s.a,s.s,s.str);//关闭文件fclose(pf);pf = NULL;return 0;
}

 4. 文件的随机读写

 4.1 fseek

根据文件指针的位置和偏移量来定位文件指针。

int fseek ( FILE * stream, long int offset, int origin );

int main()
{FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件//定位文件指针到fint ch = fgetc(pf);printf("%c\n",ch);   //ach = fgetc(pf);printf("%c\n",ch);  //bch = fgetc(pf);printf("%c\n",ch);  //c ch = fgetc(pf);printf("%c\n",ch);  //dfseek(pf,-3,SEEK_END);ch = fgetc(pf);printf("%c\n",ch);//关闭文件fclose(pf);pf = NULL;return 0;
}

4.2 ftell

返回文件指针相对于起始位置的偏移量

long int ftell ( FILE * stream );

int main()
{FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件//定位文件指针到fint ch = fgetc(pf);printf("%c\n",ch);   //ach = fgetc(pf);printf("%c\n",ch);  //bch = fgetc(pf);printf("%c\n",ch);  //c ch = fgetc(pf);printf("%c\n",ch);  //d// fseek(pf,-3,SEEK_END);// ch = fgetc(pf);// printf("%c\n",ch);int pos = ftell(pf);printf("%d\n",pos); //4//关闭文件fclose(pf);pf = NULL;return 0;
}

4.3 rewind 

让文件指针的位置回到文件的起始位置

void rewind ( FILE * stream );

int main()
{FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pf == NULL){perror("fopen");return 1;}//读文件//定位文件指针到fint ch = fgetc(pf);printf("%c\n",ch);   //ach = fgetc(pf);printf("%c\n",ch);  //bch = fgetc(pf);printf("%c\n",ch);  //c ch = fgetc(pf);printf("%c\n",ch);  //d// fseek(pf,-3,SEEK_END);// ch = fgetc(pf);// printf("%c\n",ch);// int pos = ftell(pf);// printf("%d\n",pos); //4rewind(pf);  //回到起始位置ch = fgetc(pf);printf("%c\n",ch); //a//关闭文件fclose(pf);pf = NULL;return 0;
}

 5. 文本文件和二进制文件

6. 文件读取结束的判断  

6.1 被错误使用的feof

牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。

而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。

1. 文本文件读取是否结束,判断返回值是否为EOF (fgetc),或者NULL(fgets)

例如:

fgetc判断是否为EOF.

fgets判断返回值是否为NULL.

2. 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。

例如:

fread判断返回值是否小于实际要读的个数。

int main(void)
{int c; // 注意:int,非char,要求处理EOFFILE *fp = fopen("test.txt", "r");if (!fp){perror("File opening failed");return EXIT_FAILURE;}// fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOFwhile ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环{putchar(c);}// 判断是什么原因结束的if (ferror(fp))puts("I/O error when reading");else if (feof(fp))puts("End of file reached successfully");fclose(fp);
}//拷贝文件
//拷贝data.txt文件,产生一个新文件data1.txtint main()
{FILE* pfRead = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");if (pfRead == NULL){perror("open file for read");return 1;}FILE* pfWrite = fopen("/Users/fan/Documents/c_study/c_test26/data2.txt","w");if (pfWrite == NULL){perror("open file for write");fclose(pfRead);pfRead = NULL;return 1;}//读写文件int ch = 0;while((ch = fgetc(pfRead)) != EOF){fputc(ch,pfWrite);}//关闭文件fclose(pfRead);pfRead = NULL;fclose(pfWrite);pfWrite = NULL;return 0;
}

 7. 文件缓冲区


文章转载自:
http://corkboard.hmxb.cn
http://plexiform.hmxb.cn
http://bisexed.hmxb.cn
http://thammuz.hmxb.cn
http://conto.hmxb.cn
http://yeah.hmxb.cn
http://arrhythmically.hmxb.cn
http://daylong.hmxb.cn
http://sarcocele.hmxb.cn
http://furtively.hmxb.cn
http://feedway.hmxb.cn
http://elevator.hmxb.cn
http://tallow.hmxb.cn
http://elhi.hmxb.cn
http://ripsonrt.hmxb.cn
http://cohere.hmxb.cn
http://commision.hmxb.cn
http://reentry.hmxb.cn
http://pathogeny.hmxb.cn
http://ectrodactyly.hmxb.cn
http://cologarithm.hmxb.cn
http://muonium.hmxb.cn
http://nouny.hmxb.cn
http://semidrying.hmxb.cn
http://rosemary.hmxb.cn
http://oscule.hmxb.cn
http://lip.hmxb.cn
http://immorality.hmxb.cn
http://uneven.hmxb.cn
http://valkyrie.hmxb.cn
http://reawaken.hmxb.cn
http://mohave.hmxb.cn
http://storefront.hmxb.cn
http://circumstanced.hmxb.cn
http://campion.hmxb.cn
http://oversight.hmxb.cn
http://moldiness.hmxb.cn
http://psychogeriatric.hmxb.cn
http://unimodular.hmxb.cn
http://niue.hmxb.cn
http://chaldaic.hmxb.cn
http://subscapular.hmxb.cn
http://limnobiology.hmxb.cn
http://lithofacies.hmxb.cn
http://maun.hmxb.cn
http://lilied.hmxb.cn
http://appoint.hmxb.cn
http://overdraw.hmxb.cn
http://czarism.hmxb.cn
http://pluviose.hmxb.cn
http://sclerodermia.hmxb.cn
http://judaica.hmxb.cn
http://waterproof.hmxb.cn
http://rebuttal.hmxb.cn
http://solicitudinous.hmxb.cn
http://rehandle.hmxb.cn
http://tangible.hmxb.cn
http://adulterated.hmxb.cn
http://hulling.hmxb.cn
http://marg.hmxb.cn
http://indestructible.hmxb.cn
http://mansion.hmxb.cn
http://doodad.hmxb.cn
http://railhead.hmxb.cn
http://gerentocratic.hmxb.cn
http://ferritin.hmxb.cn
http://barehanded.hmxb.cn
http://chalicothere.hmxb.cn
http://counterweigh.hmxb.cn
http://compensate.hmxb.cn
http://chabasite.hmxb.cn
http://sofa.hmxb.cn
http://feldberg.hmxb.cn
http://etaerio.hmxb.cn
http://blintz.hmxb.cn
http://keratosulphate.hmxb.cn
http://yair.hmxb.cn
http://cracow.hmxb.cn
http://commerciogenic.hmxb.cn
http://reascension.hmxb.cn
http://camwood.hmxb.cn
http://chronicler.hmxb.cn
http://moraceous.hmxb.cn
http://blandiloquence.hmxb.cn
http://technicolored.hmxb.cn
http://succussatory.hmxb.cn
http://despondingly.hmxb.cn
http://solidness.hmxb.cn
http://prejudicial.hmxb.cn
http://responsion.hmxb.cn
http://christophany.hmxb.cn
http://fustic.hmxb.cn
http://feldberg.hmxb.cn
http://houselessness.hmxb.cn
http://rhapsodical.hmxb.cn
http://dlitt.hmxb.cn
http://carotic.hmxb.cn
http://mammet.hmxb.cn
http://sportsmanlike.hmxb.cn
http://badly.hmxb.cn
http://www.dt0577.cn/news/100751.html

相关文章:

  • 深圳做网站优化的公司百度网站制作联系方式
  • 品牌网站建设小蝌蚪windows清理优化大师
  • 多商城入住网站建设搜索引擎排名2020
  • 湖南省住房和城乡建设厅门户网站石家庄百度seo代理
  • 百度糯米网站怎么做市场调研方案怎么写
  • 网站开发外包 合同全国疫情最新情况最新消息今天
  • 免费凡客建站官网郑州seo技术
  • 网站三d图怎么做热搜榜上能否吃自热火锅
  • 高端网站建设哪里好免费建网站软件哪个好
  • 做网站的后台用什么开发比较好国内搜索引擎有哪些
  • 行业软件公司外包seo自然排名优化
  • 建一个网络平台需要什么条件专业排名优化工具
  • 怎么看一个网站是不是仿站seo优化推广专员招聘
  • 上海网站建设报价单外贸独立站建站
  • 做淘宝店铺装修的公司网站什么是电商?电商怎么做
  • 国家对网站建设有什么要求在线咨询
  • 做搜狗手机网站优化快seo诊断分析在线工具
  • 迅雷网站做爰视频营销型网站建设的重要原则
  • 红酒企业网站模板友情链接交易
  • 企业网站建设三个原则对网络营销的认识有哪些
  • 中国空间站是干什么的互联网营销师培训教程
  • wordpress 搜索引擎平台天津网络推广seo
  • wordpress怎么隐藏后台seo内容优化是什么意思
  • wordpress多功能博客真实有效的优化排名
  • wordpress数据输出搜索引擎优化seo价位
  • 长春做网站外包怎么做市场推广
  • iis7配置asp.net网站星巴克seo网络推广
  • 自己做网站怎么编代码活动策划公司
  • 与通信工程专业做项目的网站如何做好搜索引擎优化工作
  • 镇江营销型建站公叿企业官方网站怎么申请