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>
int
main()
{
/*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>
int
main()
{
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) otherwise
void 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 4096
int
main()
{
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 ...
随机推荐
- 条件编译#ifdef的妙用详解
c语言中条件编译相关的预编译指令,包括 #define.#undef.#ifdef.#ifndef.#if.#elif.#else.#endif.defined. #define ...
- php中获得数组长度的方法
php中获得数组长度的方法 count统计数组里元素的个数: strlen是统计数组中元素的长度: 你如果想统计数组中所有元素的长度,那就用循环统计吧tqeb 代码: $a = array( ...
- RenderScript多输入参数
https://stackoverflow.com/questions/20783830/how-to-use-renderscript-with-multiple-input-allocations ...
- SQL学习笔记之B+树的几点总结
本文主要以列表形式将B+树的特点以及注意点等列出来,主要参考<算法导论>.维基百科.各大博客的内容,结合自己的理解写的,如内容有不当之处,请各位雅正. 0x00 前言 B树是为磁盘或其他直 ...
- Python面试题之Python正则表达式re模块
一.Python正则表达式re模块简介 正则表达式,是一门相对通用的语言.简单说就是:用一系列的规则语法,去匹配,查找,替换等操作字符串,以达到对应的目的:此套规则,就是所谓的正则表达式.各个语言都有 ...
- SaltStack部署配置Tomcat-第三篇
实验目标 简单部署tomcat及安装java环境 实现步骤 编写salt的状态模块 [root@linux-node1 web]# pwd /srv/salt/base/web [root@linux ...
- Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
一.linux中配置redis,使用java连接测试时报错: Exception in thread "main" redis.clients.jedis.exceptions.J ...
- 【转】asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法
最近我们运营的网站有用户反馈在 IE 11 下<asp:LinkButton> 点击出现 “__doPostBack”未定义”,经过一番google,终于知道了原因:ASP.NET 可能无 ...
- Android -- 文件上传到服务器
1. 文件上传的两种方式 (1) HttpClient (2)AsyncHttpClient (开源框架: https://github.com/loopj/android-async-http) 示 ...
- Ubuntu16.04 安装openssl
1 下载 https://www.openssl.org/source/ 2 解压 3 安装 # ./config --prefix=/usr/local --openssldir=/usr/loca ...