打开文件,打开文件一定要成对写,养成好习惯很重要。比如 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. Android——横屏和竖屏的切换,以及明文密码的显示

    查看API文档: android.content.pm.ActivityInfo    在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏. ...

  2. 无法激活服务,因为它不支持 ASP.NET 兼容性

    wcf错误直接上图: 原因: 一般是因为程序添加了启用了AJAX的WCF服务而缺少相关设置出现的错误. 解决方案: 1.webconfig <system.serviceModel> &l ...

  3. bat批处理延迟运行脚本(zz)

    @echo off:aaapause 这里是你需要运行的程序for /l %%i in (0,1,10000) do echo %%i>nulgoto aaa 当然bat延迟运行还有其他的一些方 ...

  4. Java常用类库

    System System:类中的方法和属性都是静态的. out:标准输出,默认是控制台. in:标准输入,默认是键盘. System描述系统一些信息.获取系统属性信息:Properties getP ...

  5. 天使投资、VC 以及 PE 的区别是什么?

    如果满足于“阶段不同”这个简单的回答,那你可能错过了一个思考资本与企业发展之间关系的机会. 首先要交待一下,在大众语境中,angel/VC/PE三者都可认为是VC,也就是人们常说的风险投资,在国内官方 ...

  6. MCU晶体旁边电容的作用及振荡电路的分析

    绝大多数的MCU爱好者对MCU晶体两边要接一个22pF附近的电容不理解,因为这个电容有些时候是可以不要的.参考很多书籍,讲解的很少,往往提到最多的是起稳定作用,负载电容之类的话,都不是很深入理论的分析 ...

  7. updmap-sys failed. Output has been stored in

    Ubuntu 12.04升级到Ubuntu 12.04lts的时候,出现错误: Do you want to continue? [Y/n] ySetting up tex-common (4.04) ...

  8. MyBatis学习总结_08_Mybatis3.x与Spring4.x整合

    一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...

  9. 120条Photoshop新手必看技巧

    Photoshop越来越强大了!试图掌控它的全部特性是不现实的(更何况有那么多隐藏的功能!),那么我们不妨收藏一下大神们总结的这120个PS技巧,偶尔翻看一下,让自己的设计更强大更高效! 这120款技 ...

  10. USACO Section 2.3: Money Systems

    简单的dp题 /* ID: yingzho1 LANG: C++ TASK: money */ #include <iostream> #include <fstream> # ...