APUE学习笔记——5.5~5.7数据流的打开与读写
1.open
#include <stdio.h>FILE *fopen(const char *restrict pathname,const char *restrict type)FILE *freopen(const char *restrict pathname,const char *restrict type,FILE *restrict fp);FILE *fdopen(int fd,const char *type);--All three return: file pointer if OK,NULLon error

example:
#include<stdio.h>intmain(){/*redirect standard output to file test.out*/if(freopen("./test.out","w",stdout)==NULL)fprintf(stderr,"errorredirectingstdout\n");/*this output will go to a file test.out*/printf("Hello World\n");/*close the standard output stream*/fclose(stdout);return 0;}
#include<stdio.h>intmain(){FILE *fp = fdopen(0,"w+");fprintf(fp,"%s\n", "Hello world!");fp =fdopen(1,"w+");fprintf(fp,"%s\n","Hello world!");fclose(fp);}
2.read and write
#include <stdio.h>int getc(FILE *fp);int fgetc(FILE *fp);int getchar(void);All three return: next character if OK,EOFon end of file or error
#define getc(_fp) _IO_getc (_fp)
#include <stdio.h>int ferror(FILE *fp);int feof(FILE *fp);Both return: nonzero(true) if condition is true, 0 (false) otherwisevoid clearerr(FILE *fp);
#include <stdio.h>int ungetc(intc,FILE *fp);Returns:cif OK,EOFon error
back回去
#include <stdio.h>int putc(intc,FILE *fp);int fputc(intc,FILE *fp);int putchar(intc);All three return:cif OK,EOFon error
行缓冲 Line-at-a-Time I/O
#include <stdio.h>char *fgets(char *restrict buf,int n,FILE *restrict fp);char *gets(char *buf);Both return:buf if OK,NULL on end of file or error
也就是我们如果在输入流中输入Hello然后回车结束,且n不小于7,那么buf中的数据就是
#include<stdio.h>#define BUFFSIZE 4096intmain(){char buf[BUFFSIZE];fgets(buf,5,stdin);puts(buf);fgets(buf,10,stdin);puts(buf);return 0;}~
#include <stdio.h>int fputs(const char *restrict str,FILE *restrict fp);int puts(const char *str);Both return: non-negative value if OK,EOFon error
APUE学习笔记——5.5~5.7数据流的打开与读写的更多相关文章
- APUE学习笔记——10.9 信号发送函数kill、 raise、alarm、pause
转载注明出处:Windeal学习笔记 kil和raise kill()用来向进程或进程组发送信号 raise()用来向自身进程发送信号. #include <signal.h> int k ...
- APUE学习笔记3_文件IO
APUE学习笔记3_文件IO Unix中的文件IO函数主要包括以下几个:open().read().write().lseek().close()等.这类I/O函数也被称为不带缓冲的I/O,标准I/O ...
- apue学习笔记(第一章UNIX基础知识)
总所周知,UNIX环境高级编程是一本很经典的书,之前我粗略的看了一遍,感觉理解得不够深入. 听说写博客可以提高自己的水平,因此趁着这个机会我想把它重新看一遍,并把每一章的笔记写在博客里面. 我学习的时 ...
- APUE学习笔记-一些准备
从开始看APUE已经有快一个星期了,由于正好赶上这几天清明节放假,难得有了三天空闲假期可以不受打扰的学习APUE,现在已经看完前六章了,里面的大部分例程也都亲自编写,调试过了.但总觉得这样学过就忘,因 ...
- APUE学习笔记(2):lseek()练习与文件洞
对于lseek函数早在大一的C语言课上就有接触,但是几乎没有使用过,只记得是和文件偏移操作相关的 看了APUE上的示例,又使用od工具查看了内容,果然很神奇,很新鲜 figure3.2.c [c] # ...
- APUE学习笔记(1):APUE运行环境
APUE全称<Advanced Programming in the UNIX Environment>(UNIX环境高级编程) 书中例子大都使用作者自己写的头文件,所以需要解决一下,还好 ...
- apue学习笔记(第十七章 高级进程间通信)
本章介绍一种高级IPC---UNIX域套接字机制,并说明它的应用方法 UNIX域套接字 UNIX域套接字用于在同一台计算机上运行的进程(无关进程)之间的(全双工)通信.相比于因特网套接字,UNIX域套 ...
- apue学习笔记(第十六章 网络IPC:套接字)
本章将考察不同计算机(通过网络连接)上的进程相互通信的机制:网络进程间通信. 套接字描述符 正如使用文件描述符访问文件,应用程序用套接字描述符访问套接字. 许多处理文件描述符函数(如read和writ ...
- kinect学习笔记(四)——各种数据流
一.kinect开发的一个流程图 1.我们可以知道一个简单的框架就是几部分 (1)选择使用的kinect传感器 KinectSensor.KinectSensors[] (2)打开需要的数据流 _ki ...
随机推荐
- mysql服务解压版的安装(5.7)
推荐博客: https://www.cnblogs.com/LxyXY/p/7708016.html
- composer错误_Content-Length mismatch, received 84697 bytes out of the expected..
使用composer下载源码出现错误 [Composer\Downloader\TransportException] Content-Length mismatch, received bytes ...
- Centos下Nginx配置WEB访问日志并结合shell脚本定时切割
在一个成熟的WEB系统里,没有日志管理是不可以的,有了日志,可以帮助你得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某个服务或server的性能瓶颈等. ...
- Python3.x:正则 re.findall()的用法
Python3.x:正则 re.findall()的用法 概念: 语法:findall(pattern, string, flags=0) 说明:返回string中所有与pattern相匹配的全部字串 ...
- MVC中 关于退出按钮的写法
public ActionResult Logoff() { Session.Abandon(); Session.Clear(); FormsAuthentication.SignOut(); re ...
- jQuery height() 需要注意的地方
var aNode = $('#id'); var height = aNode.height(); //如果在获取height前,aNode已经是display:none 或者说 aNode是隐藏的 ...
- 深入 CommonJs 与 ES6 Module
目前主流的模块规范 UMD CommonJs es6 module umd 模块(通用模块) (function (global, factory) { typeof exports === 'obj ...
- 组合优于继承 Composition over inheritance
https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance 解答1 Prefer composition ...
- Apache Kafka之设计
转自: http://blog.csdn.net/kevin_hx001/article/details/9413565 http://kafka.apache.org/design.h ...
- Class 的基本语法
简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y = y; } Point ...