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

品牌型网站仿牌外贸网站制作

品牌型网站,仿牌外贸网站制作,东营网格通,三只松鼠广告策划书目录 一、回顾C语言文件操作二、文件系统调用接口1. open2.write3.read 三、文件描述符四、重定向1.输出重定向2.输入重定向 五、dup2 一、回顾C语言文件操作 1 #includestdio.h2 #includestdlib.h3 4 #define LOG log.txt5 6 int main()7 {8 //… 目录 一、回顾C语言文件操作二、文件系统调用接口1. open2.write3.read 三、文件描述符四、重定向1.输出重定向2.输入重定向 五、dup2 一、回顾C语言文件操作 1 #includestdio.h2 #includestdlib.h3 4 #define LOG log.txt5 6 int main()7 {8 //w 默认写方式打开文件如果文件不在就创建它9 //默认如果是打开内容会被自动清空10 //同时每次进行写入的时候都会从最开始写入11 FILE* fpfopen(LOG,w); //r只读 a追加12 if(fpNULL)13 {14 perror(fopen); //判断是否创建成功15 return 1;16 }17 18 //文件打开成功进行文件操作19 const char* msghello linux!;20 int cnt5;21 while(cnt)22 {23 fprintf(fp,%s: %d: tzc\n,msg,cnt); //打印到文件中 24 //fprintf(stdout,%s: %d: tzc\n,msg,cnt); //stdout 打印到显示器文件中 25 //fputs(msg,fp);26 cnt--;27 }28 fclose(fp); //关闭文件29 30 return 0;31 } C语言有三个默认输入输出流分别是 stdinstdout,stderr.将他们分别称为标准输入标准输出标准错误他们分别对应 键盘、显示器、显示器。   代码 #includestdio.hint main() { const char* str hello wrold!\n;fputs(str,stdout); // 向标准输出中打印即打印在显示器上fputs(str,stdout);fputs(str,stdout); }运行结果 二、文件系统调用接口 1. open open打开文件后会返回一个文件描述符用来后续进行文件操作 #includestdio.h #includeunistd.h #includesys/stat.h #includesys/types.h #includefcntl.hint main() {umask(0); // 防止umask码设置权限影响期望权限int fd open(./log.txt,O_WRONLY|O_CREAT,0666); // 以只写方式打开如果没有该文件就创建权限为666if(fd0) // 打开失败{perror(open);return 1;}close(fd);return 0; }2.write 第一个参数是需要填写文件描述符需要操作哪一个文件就将哪一个文件的文件描述符填到第一个参数第二个参数是要写入的内容第三个参数是写入的大小 返回值返回值为实际写入文件数据大小。 1 #includestdio.h2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 int main()9 {10 int fd open(./log.txt,O_WRONLY|O_CREAT,0644);11 if(fd0)12 {13 perror(open);14 return 1;15 }16 17 const char* msg hello linux!\n;18 int count 5;19 while(count--)20 {21 char line[256]; //缓存数组22 snprintf(line,sizeof(line),%s, %d\n,msg,count); //写入line 23 write(fd, msg,strlen(msg));// 写入时不需要写入\0字符串以\0结束只是C语言的规定文件中字符串不需要\024 }25 close(fd);26 return 0;27 } 3.read 第一个参数是文件描述符第二个参数是读取到哪个位置第三个参数是读取多大 需要注意的是读取的时候直接全文进行读取不会按行读取。 1 #includestdio.h 2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 9 int main()10 {11 int fd open(./log.txt,O_RDONLY);12 if(fd0)13 {14 perror(open);15 return 1;16 }17 18 char buf[1024];19 ssize_t s read(fd, buf, sizeof(buf)-1);// 将文件内容读出需将字符串末尾加\020 if(s0)21 {22 buf[s] 0;23 printf(%s\n,buf); //将读取到的字符串打印出来24 }25 else26 {27 printf(read failed\n);28 }29 return 0;30 } 三、文件描述符 文件描述符就是调用系统接口open的返回值打开成功返回该文件的文件描述符打开失败返回-1。 #includestdio.h #includeunistd.h #includesys/stat.h #includesys/types.h #includefcntl.hint main() {int fd1 open(./log.txt,O_WRONLY|O_CREAT,0644);int fd2 open(./log.txt,O_WRONLY|O_CREAT,0644);int fd3 open(./log.txt,O_WRONLY|O_CREAT,0644);int fd4 open(./log.txt,O_WRONLY|O_CREAT,0644);printf(fd1: %d\n,fd1);printf(fd2: %d\n,fd2);printf(fd3: %d\n,fd3);printf(fd4: %d\n,fd4);return 0; }实际上文件描述符就是一个数组的下标如何理解 系统的标准输入标准输出和标准错误占据了数组的前三个位置所以我们进程在打开文件的时候默认文件描述符就是从3开始。   如何验证 四、重定向 1.输出重定向 1 #includestdio.h2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 int main()9 {10 close(1); // 关闭标准输出11 int fd open(./log.txt, O_WRONLY|O_CREAT|O_TRUNC,0644);12 // int fd open(./log.txt, O_WRONLY|O_CREAT|O_APPEND,0644); // 追加重定向 只是换成append 13 if(fd0)14 { 15 perror(open); 16 return 1; 17 } 18 19 int count 5; 20 while(count--) 21 { 22 printf(hello world!\n); 23 } 24 25 return 0; 26 } 原理 2.输入重定向 1 #includestdio.h2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 int main()9 {10 close(0); // 关闭标准输入11 int fd open(./log.txt, O_RDONLY);12 if(fd0)13 {14 perror(open);15 return 1; 16 }17 18 int a,b;19 scanf(%d %d,a,b);20 printf(a%d, b%d\n,a,b);21 22 return 0;23 } 原理跟输出重定向相同不过要关闭的是stdin 除了代码方式我们可以通过指令方式进行重定向 1 #includestdio.h2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 int main()9 {10 printf(you can see me!\n);11 printf(you can see me!\n);12 printf(you can see me!\n);13 printf(you can see me!\n);14 15 fprintf(stdout,hello linux!\n);16 fprintf(stdout,hello linux!\n);17 fprintf(stdout,hello linux!\n);18 19 fprintf(stderr,hello tzc!\n);20 fprintf(stderr,hello tzc!\n); 21 fprintf(stderr,hello tzc!\n);22 23 24 return 0;25 }将原本输出到显示器文件中的数据重定向到log.txt中重定向stdout的内容所以stderr中的内容还是被打印到显示器文件中了 如果要对标准错误stderr的内容重定向到文件中可以另加修饰。 此时我们可以看到标准错误和标准输出都打印到同一个文件中。 也可以进行分开重定向 五、dup2 我们发现在对数据进行重定向的时候操作比较复杂系统提供了一个函数用来简化重定向操作   dup2有两个参数oldfd和newfd,他的底层原理实际是将newfd的指针覆盖换成指定oldfd位置的指针 使用 1 #includestdio.h2 #includeunistd.h3 #includesys/stat.h4 #includesys/types.h5 #includefcntl.h6 #includestring.h7 8 #define LOG log.txt9 10 int main()11 {12 int fdopen(LOG,O_WRONLY|O_CREAT|O_TRUNC,0666); //打开文件13 if(fd-1)14 {15 perror(open);16 return -1;17 }18 19 dup2(fd,1); //将下标为1位置的内容覆盖为fd位置下标中内容20 21 printf(hello linux!\n);22 printf(hello linux!\n);23 printf(hello linux!\n); 24 25 return 0;26 }
http://www.dnsts.com.cn/news/136913.html

相关文章:

  • 请打开网站建造师官网
  • 什么网站可以做市场分析呢网站建设做的好
  • 太原做网站公司哪家好网站续费要多少钱
  • 广州文化网站模板安徽省住房和城乡建设工程信息网
  • 天津河西做网站哪家好电商网站开发文献汇总
  • 网站怎么做微信支付功能工业物联网平台
  • 网页设计网站哪个公司好docker 部署wordpress
  • 未来中森网站建设价格wordpress无法保存pages
  • 网站建设合同属于技术服务么最近的新闻摘抄
  • 可以做软件外包项目的网站无锡市住房和城乡建设部网站
  • wordpress数据库损坏网站网站怎么做h5支付宝支付
  • 网站开发要什么流程数据库端口 wordpress
  • 网站建设广告费 科目萧山网站建设
  • 网站上线后的工作品牌建设还需持续力
  • 静态页优秀网站网站建设经验交流材料
  • 健康东莞app怎么下载seozou是什么意思
  • 江宁网站建设案例市场调研怎么写
  • 怎样在文章后做网站链接专业的商城网站开发
  • wordpress禁用头像郑州网站优化推广培训
  • 阿里云服务器网站开发网络营销论文答辩提问
  • 东莞市网站建设制作设计平台宁波市房产交易信息服务网
  • 顺德网站建设报价管理系统oa
  • 上海做网站的小公司小小影视大全在线观看免费观看
  • 网站免费建站广告机做展示网站要恋用什么程序
  • 制作app的网站哪个好泰州制作公司网站
  • 网站推广的主题北京南站到北京西站地铁怎么走
  • 专业网站建设服务公司哪家好东莞推广系统怎么做
  • 酒泉市建设局网站招标办海外营销推广 平台
  • 旅游景区网站模板让家里的电脑做网站服务器
  • 网站建设验收网络培训远程教育平台