打开文件,打开文件一定要成对写,养成好习惯很重要。比如 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. group_concat

    分割字符串 group_concat(a.EvidencesID separator ',') as EvidencesID #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为 ...

  2. HDU 1392 Surround the Trees (Graham求凸包周长)

    题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...

  3. tomcat下context.xml中JNDI数据源配置

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  4. linq lambda 分组后排序

    1.lamdba分组排序foodBusinessDistrict.                        GroupBy(x => new                        ...

  5. sql 数据库换行

    制表符 CHAR(9)  换行符 CHAR(10)  回车 CHAR(13) 

  6. iOS开发日期处理

    Foundation框架之 日期与时间 #import"ViewController.h"@interfaceViewController() { NSTimer*_timer;/ ...

  7. Ajax实例-购物车

    一.概述 1.当添加或删除商品时,购物车会立即更新数据 2.思路: (1)建立商品类Item.java,存有商品属性name,prince,code(商品编码)等 (2)建立商品目录类Catalog. ...

  8. android中使用html作布局文件

    在android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...

  9. Centos 用户组管理

    #组帐号管理 linux 组管理 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--= 1.组的分类 私用组:只能包含一个用 ...

  10. Android 使用意图传递数据

    使用意图传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <But ...