打开文件,打开文件一定要成对写,养成好习惯很重要。比如 fopen()fclose
<ol>
<li>fopen()</li>

<pre lang="c" escaped="true">
/* fopen() */
FILE *fopen(const char *path, const char *mode);

FILE *fdopen(int fd, const char *mode);

FILE *freopen(const char *path, const char *mode, FILE *stream);
</pre>

mode:

<strong>r</strong> Open text file for reading. The stream is positioned at the beginning of the file.

<strong>r+</strong> Open for reading and writing. The stream is positioned at the beginning of the file.

<strong>w</strong> Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

<strong>w+</strong> Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the
file.

<strong>a</strong> Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

<strong>a+</strong> Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the
beginning of the file, but output is always appended to the end of the file.

&nbsp;
&nbsp;

<li>fclose()</li>

<pre lang="c" escaped="true">
/* fclose() */
int fclose(FILE *stream);
</pre>

RETURN VALUE
Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case any further access (including
another call to fclose()) to the stream results in undefined behavior.

<li>EOF</li>

<pre lang="c" escaped="true">
/* End of file character.
* some things throughout the library rely on this being -1. */
#ifndef EOF
#define EOF (-1)
#endif

/* stdin/stdout/stderr */
stdin
stdout
stderr
</pre>

<li>errno</li>

<pre lang="c" escaped="true">
/* errno */
void perror(const char *s);
char *strerror(int errnum);

int strerror_r(int errnum, char *buf, size_t buflen);
/* XSI-compliant */

char *strerror_r(int errnum, char *buf, size_t buflen);
/* GNU-specific */

char *strerror_l(int errnum, locale_t locale);

fprintf(stderr, "filename:%s, line %d: %s.\n", __FILE__, __LINE__, strerror(errno));
</pre>

<li>fgetc() fputc()</li>

<pre lang="c" escaped="true">
/* get char */
int fgetc(FILE *stream);

char *fgets(char *s, int size, FILE *stream);

int getc(FILE *stream);

int getchar(void);

int ungetc(int c, FILE *stream);

/* put char */
int fputc(int c, FILE *stream);

int fputs(const char *s, FILE *stream);

int putc(int c, FILE *stream);

int putchar(int c);

int puts(const char *s);
</pre>

举个例子如下:
<pre lang="c" escaped="true">
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *fp;
int ch;

if ((fp = fopen("test2", "w+")) == NULL)
{
perror("Open file test2.\n");
exit(1);
}

while ((ch = getchar()) != EOF)
fputc(ch, fp);
rewind(fp);
while((ch = fgetc(fp)) != EOF)
putchar(ch);

fclose(fp);

return 0;
}
</pre>

运行结果:

<blockquote>
[sbso@localhost c]$ gcc fputc_fgetc_test.c -o fputc_fgetc_test
[sbso@localhost c]$ ./fputc_fgetc_test
hello world. www.emacsvi.com
must enter ctrl-d over the input.
ok goodbye.
hello world. www.emacsvi.com
must enter ctrl-d over the input.
ok goodbye.
[sbso@localhost c]$
[sbso@localhost c]$ cat test2
hello world. www.emacsvi.com
must enter ctrl-d over the input.
ok goodbye.
[sbso@localhost c]$
</blockqoute>

标准IO库函数复习的更多相关文章

  1. C 标准IO 库函数与Unbuffered IO函数

    先来看看C标准I/O库函数是如何用系统调用实现的. fopen(3) 调用open(2)打开指定的文件,返回一个文件描述符(就是一个int 类型的编号),分配一 个FILE 结构体, 通常里面包含了: ...

  2. linux标准io的copy

    ---恢复内容开始--- 1.linux标准io的copy #include<stdio.h> int main(int argc,char **argv) { if(argc<3) ...

  3. UNIX高级环境编程(6)标准IO函数库 - 流的概念和操作

    标准IO函数库隐藏了buffer大小和分配的细节,使得我们可以不用关心预分配的内存大小是否正确的问题. 虽然这使得这个函数库很容易用,但是如果我们对函数的原理不熟悉的话,也容易遇到很多问题.   1 ...

  4. 为什么需要标准IO缓冲?

    (转)标准I/O缓冲:全缓冲.行缓冲.无缓冲 标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的 ...

  5. Linux 标准IO库介绍

    1.标准IO和文件IO有什么区别? (1).看起来使用时都是函数,但是:标准IO是C库函数,而文件IO是Linux系统的API. (2).C语言库函数是由API封装而来的.库函数内部也是通过调用API ...

  6. 深究标准IO的缓存

    前言 在最近看了APUE的标准IO部分之后感觉对标准IO的缓存太模糊,没有搞明白,APUE中关于缓存的部分一笔带过,没有深究缓存的实现原理,这样一本被吹上天的书为什么不讲透彻呢?今天早上爬起来赶紧找了 ...

  7. 标准io与文件io

    A: 代码重复: 语句块1: while(判断) { 语句块2: 语句块1: } 上面可以改写为: while(1) { 语句块1: if(判断) break: 语句块2: } B: 标准IO和文件I ...

  8. 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口

    最近学习linux系统下的应用编程,参考书籍是那本称为神书的<Unix环境高级编程>,个人感觉神书不是写给草鞋看的,而是 写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻 ...

  9. (九)errno和perror、标准IO

    3.1.6.文件读写的一些细节3.1.6.1.errno和perror(1)errno就是error number,意思就是错误号码.linux系统中对各种常见错误做了个编号,当函数执行错误时,函数会 ...

随机推荐

  1. 安装微软ASP.NET MVC 4,运行以下的包管理器控制台命令

    (菜鸟,勿喷,有错求指正)Asp.net  新建的类库中安装MVC4  .下面是步骤,1+2:打开程序包管理控制台,3:运行Install-Package Microsoft.AspNet.Mvc - ...

  2. MyEclipse编码设置

    (1)windows---->Preferences (2)general---->Workspace (3)设置编码

  3. poj 1085 Triangle War 博弈论+记忆化搜索

    思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...

  4. Linux客户/服务器程序设计范式2——并发服务器(进程池)

    引言 让服务器在启动阶段调用fork创建一个子进程池,通过子进程来处理客户端请求.子进程与父进程之间使用socketpair进行通信(为了方便使用sendmsg与recvmsg,如果使用匿名管道,则无 ...

  5. Linux网络编程8——对TCP与UDP的简易封装

    引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...

  6. poj 3522(最小生成树应用)

    题目链接:http://poj.org/problem?id=3522思路:题目要求最小生成树中最大边与最小边的最小差值,由于数据不是很大,我们可以枚举最小生成树的最小边,然后kruskal求最小生成 ...

  7. 【poj1006-biorhythms】中国剩余定理

    http://poj.org/problem?id=1006 题意:中国剩余定理的裸题. 题目可转化为求最小的x满足以下条件: x%23=a;x%28=b;x%33=c; 关于中国剩余定理可看我昨天的 ...

  8. 模拟+思维 HDOJ 5319 Painter

    题目传送门 /* 题意:刷墙,斜45度刷红色或蓝色,相交的成绿色,每次刷的是连续的一段,知道最终结果,问最少刷几次 模拟+思维:模拟能做,网上有更巧妙地做法,只要前一个不是一样的必然要刷一次,保证是最 ...

  9. 12个QT基本对话框,以及淡入原理(用定时器把窗口逐渐变成透明)

    一.基本对话框 1,核心库: 界面程序 QApplication 非程序界面QCoreAppliction 2,消息循环必须执行QApplication.exec(); 3,消息绑定机制: 信号-槽  ...

  10. TWinControl与TControl的覆盖函数(TWinControl对TControl的10个消息覆盖函数,17个覆盖函数,私有虚函数仍可多态)

    手工找出来,对比一下,有助于VCL框架的理解.----------------------------------------------------------------------------- ...