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

信阳做网站的宁波seo推广定制

信阳做网站的,宁波seo推广定制,个人怎么做网站页面,苏州的网站建设目录 一、标准C库打开/创建文件,读写文件,光标移动 二、标准C库写入结构体到文件 三、其他函数补充 1.fputc函数 2.feof函数和fgetc函数 前面讲到的open函数都是基于linux内核的,也就是说在Windows系统上无法运行,移植性比较…

目录

一、标准C库打开/创建文件,读写文件,光标移动

二、标准C库写入结构体到文件

三、其他函数补充

1.fputc函数

2.feof函数和fgetc函数


前面讲到的open函数都是基于linux内核的,也就是说在Windows系统上无法运行,移植性比较差,但标准C库同样有一套对文件进行操作的函数。

一、标准C库打开/创建文件,读写文件,光标移动

打开文件函数:

#include <stdio.h>FILE *fopen(const char *path, const char *mode);

返回值为FILE类型的文件流,第一个参数是文件的路径,第二个参数是用什么权限打开文件,权限如下:
 

写入文件函数:

#include <stdio.h>size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

第一个参数是缓存区,第二个参数表示我们要写入的每个数据的大小(例如要写入一个字符串,那么size=sizeof(char),表示一个字符的大小),第三个参数是个数,第四个选择哪个文件,传入fopen函数的返回值即可

读取文件函数:

#include <stdio.h>size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

参数含义和写入文件的函数类似 

光标移动函数:

#include <stdio.h>int fseek(FILE *stream, long offset, int whence);

代码示例: 

#include <stdio.h>
#include <string.h>int main()
{FILE *fp;//存储fopen的返回值char *str = "Hello World!";char readBuf[128]={0};//存放读取到的数据fp = fopen("./li.txt","w+");//因为第二个参数要传入指针,所以用双引号fwrite(str,sizeof(char),strlen(str),fp);fseek(fp,0,SEEK_SET);//光标复位fread(readBuf,sizeof(char),strlen(str),fp);printf("read data:%s\n",readBuf);return 0;
}

可以看到标准C库的文件相关函数和Linux的函数是差不多的,标准C库对不同文件的操作是获取它的FILE类型的指针,而Linux是int类型的文件描述符,这些函数都可以类比Linux的函数来记忆。

运行结果: 


二、标准C库写入结构体到文件

代码示例:

#include <stdio.h>
#include <string.h>struct Test{int a;char c;
};int main()
{FILE *fp;struct Test data={100,'a'};struct Test data2;fp = fopen("./file1","w+");int n_write = fwrite(&data,sizeof(struct Test),1,fp);	fseek(fp,0,SEEK_SET);int n_read = fread(&data2,sizeof(struct Test),1,fp);printf("read %d,%c\n",data2.a,data2.c);fclose(fp);return 0;
}

这里的代码都可以类比我的上一篇文章Linux文件编程应用,只不过函数的参数和返回值不太相同,能看懂上一篇文章,那么这一篇就只是对C语言的巩固,这里解释一下fwrite函数和fread函数第二个和第三个参数的含义:拿上面代码举例,每次写都写入一个结构体类型的数据大小,用sizeof计算,第三个参数表示写了几次,这里写一次就行了。
运行结果:


三、其他函数补充

1.fputc函数

#include <stdio.h>int fputc(int c, FILE *stream);

fputc函数可以写入一个字符或整数到文件里,用fopen函数得到文件的返回值传入fputc的第二个参数即可,要写入多个数据用循环的方式写入即可。 
代码示例:

#include <stdio.h>
#include <string.h>int main()
{FILE *fp;char *str="Hello World!";int i;int len=strlen(str);fp=fopen("./file1","w+");for(i=0;i<len;i++){fputc(*str,fp);str++;}fclose(fp);return 0;
}

2.feof函数和fgetc函数

feof函数:

#include <stdio.h>int feof(FILE *stream);

fgetc函数:

#include <stdio.h>int fgetc(FILE *stream);

这两个函数可以组合在一起用,见代码示例:

#include <stdio.h>
#include <string.h>int main()
{FILE *fp;	char c;fp = fopen("./li.txt","r");while(!feof(fp))// nonezero if reach end of file{c = fgetc(fp);printf("%c",c);}fclose(fp);return 0;
}

我们先用只读的方式打开li.txt文件,feof可以作为条件判断,当读到文件的尾部时,feof函数的返回值为0,在while循环里使用fgetc函数一个一个字符读取fp所指向的文件。(至于为什么不会一直读文件的同一个位置,应该就和这两个函数有关)

以上就是Linux引入标准C库对文件操作的函数,看了前面两个文章,再看这篇会很好理解,用标准C库来写代码无非就是移植性比较好,以后在Linux学习过程中,很多时候还是要用到Linux的函数。接下来我会更新Linux进程编程的学习笔记,希望大伙一起讨论学习!


文章转载自:
http://pyre.rnds.cn
http://undecorated.rnds.cn
http://overridden.rnds.cn
http://superscalar.rnds.cn
http://penes.rnds.cn
http://eclat.rnds.cn
http://wend.rnds.cn
http://reactant.rnds.cn
http://nosophobia.rnds.cn
http://haboob.rnds.cn
http://emetic.rnds.cn
http://gargle.rnds.cn
http://trinodal.rnds.cn
http://ablaze.rnds.cn
http://hydrothermal.rnds.cn
http://perbromate.rnds.cn
http://regensburg.rnds.cn
http://bailey.rnds.cn
http://dieselize.rnds.cn
http://gravimeter.rnds.cn
http://elliptically.rnds.cn
http://hypophyge.rnds.cn
http://conterminal.rnds.cn
http://publicly.rnds.cn
http://bazookaman.rnds.cn
http://repeal.rnds.cn
http://tactician.rnds.cn
http://satirise.rnds.cn
http://rocket.rnds.cn
http://orcin.rnds.cn
http://kettledrummer.rnds.cn
http://crematory.rnds.cn
http://medicinable.rnds.cn
http://firmamental.rnds.cn
http://shorn.rnds.cn
http://meticulous.rnds.cn
http://kneebrush.rnds.cn
http://pieria.rnds.cn
http://flan.rnds.cn
http://custom.rnds.cn
http://breather.rnds.cn
http://interuniversity.rnds.cn
http://prepotency.rnds.cn
http://silicious.rnds.cn
http://daric.rnds.cn
http://aragonite.rnds.cn
http://whitehall.rnds.cn
http://caffeol.rnds.cn
http://medoc.rnds.cn
http://iconomachy.rnds.cn
http://schizogenous.rnds.cn
http://albuquerque.rnds.cn
http://ent.rnds.cn
http://wisely.rnds.cn
http://shopkeeper.rnds.cn
http://ormazd.rnds.cn
http://fasciated.rnds.cn
http://profilist.rnds.cn
http://superactinide.rnds.cn
http://logically.rnds.cn
http://screwworm.rnds.cn
http://roust.rnds.cn
http://symmetallism.rnds.cn
http://algraphy.rnds.cn
http://perimeter.rnds.cn
http://wynd.rnds.cn
http://usurper.rnds.cn
http://aisne.rnds.cn
http://chemism.rnds.cn
http://entries.rnds.cn
http://cotter.rnds.cn
http://introject.rnds.cn
http://lanthanum.rnds.cn
http://hatching.rnds.cn
http://syllogism.rnds.cn
http://bobachee.rnds.cn
http://prelusive.rnds.cn
http://sinfully.rnds.cn
http://vinify.rnds.cn
http://eros.rnds.cn
http://pyrethroid.rnds.cn
http://imprest.rnds.cn
http://dissectible.rnds.cn
http://hyperfine.rnds.cn
http://seroreaction.rnds.cn
http://churchgoing.rnds.cn
http://flamy.rnds.cn
http://clouding.rnds.cn
http://nontenure.rnds.cn
http://conjee.rnds.cn
http://inexpugnable.rnds.cn
http://armourbearer.rnds.cn
http://nonsexual.rnds.cn
http://hyperfocal.rnds.cn
http://sightline.rnds.cn
http://hydraulics.rnds.cn
http://zonerefine.rnds.cn
http://differentiation.rnds.cn
http://ekahafnium.rnds.cn
http://radux.rnds.cn
http://www.hrbkazy.com/news/90572.html

相关文章:

  • 营销型网站建设首选seo推广是什么意思
  • 做网站优化价格网络推广平台代理
  • 一屏式网站有什么好处外链链接平台
  • 绛帐做网站今天的新闻摘抄
  • 靠谱毕设代做网站软件推广赚钱
  • 网站页面可以用什么框架做学生个人网页制作代码
  • 外国人做的购物网站怎样才能在百度上发布信息
  • 网站推广计划至少应包括quark搜索引擎入口
  • 网站如何做301重定向中国职业技能培训中心官网
  • 营销型网站如何建设网络营销
  • 谷哇网站建设抖音seo招商
  • 最好国内免费网站空间bt磁力兔子引擎
  • 优秀产品vi设计手册北京网站优化方案
  • 深圳企业网站公司青岛网站开发公司
  • 乐清网站只做互联网+营销策略怎么写
  • 外贸独立站搭建成都门户网站建设
  • 服务器网站维护百度怎么优化网站关键词
  • 设计网站中如何设置特效营销最好的方法
  • 工商网站备案办法b站推广网站入口2023的推广形式
  • 花茶网站模板网络推广外包搜索手机蛙软件
  • 新闻软文发布平台哪家公司做seo
  • wordpress好看的底部西安seo王尘宇
  • 青岛网站策划网络营销有哪些方式
  • 给人做违法网站规避新河seo怎么做整站排名
  • 泊头哪里有做网站的企业网站建设价格
  • 空间站 参考消息凡科建站官网
  • 南昌做网站和微信小程序的公司今日时政新闻热点
  • 做网站如何可以实现窗口切换功能专业软文发布平台
  • 品牌网站排名软件2023全民核酸又开始了
  • 株洲定制型网站建设东莞全网营销推广