打开文件,打开文件一定要成对写,养成好习惯很重要。比如 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. 机器学习之逻辑回归(Logistic Regression)

    1. Classification 这篇文章我们来讨论分类问题(classification problems),也就是说你想预测的变量 y 是一个离散的值.我们会使用逻辑回归算法来解决分类问题. 之 ...

  2. ANDROID STUDIO, GRADLE AND NDK INTEGRATION

    Originally posted on:http://ph0b.com/android-studio-gradle-and-ndk-integration/ With the recent chan ...

  3. GetWindowText和GetDlgItemText的区别

    二者使用方法相同,入口点不一样. 举例: CString str;  /* if (GetDlgItem(IDC_Number1)->GetWindowText(str),str==" ...

  4. 叠罗汉I

    叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上.同时我们应该让下面的人比上面的人更高一点.已知参加游戏的每个人的身高,请编写代码计算通过选择参与游戏的人,我们多能叠多少个人.注意这里的人都 ...

  5. QT为QLabel添加Click事件(如果我们使用组件,我们关心的是信号槽;如果我们自定义组件,我们关心的是事件)

    其实就是改写了一个函数:mouseReleaseEvent,当在QLabel放开鼠标的时,就发射点击信号. #ifndef CLICKEDLABEL_H_ #define CLICKEDLABEL_H ...

  6. 309. Best Time to Buy and Sell Stock with Cooldown

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  7. Hadoop namenode无法启动问题解决

    原因:在root账户(非hadoop账户)下操作hadoop会导致很大的问题 首先运行bin/start-all.sh发现namenode没有启动 只有它们 9428 DataNode 9712 Jo ...

  8. queue-fun —— nodejs下基于Promise的队列控制模块。

    工作告一段落,闲来无事,写了一个在nodejs实现“半阻塞”的控制程序. 一直以来,nodejs以单线程非阻塞,高并发的特性而闻名.搞这个“半阻塞”是东西,有什么用呢? 场景一: 现在的web应用可有 ...

  9. Docker+K8S实践

    一.运维角度: (一)镜像: 1. 避免依赖过深.不要在基础镜像上加太多产生其他的镜像,我觉得这块最多是三四层. 一层是base景像再往上是工具.中间件这样的,再往上一层就是你自己的程序,再多就比较乱 ...

  10. TFSAPI

    Team Foundation Server (TFS)工具的亮点之一是管理日常工作项, 工作项如Bug, Task,Task Case等. 使用TFS API编程访问TFS服务器中的工作项, 步骤如 ...