标准IO库函数复习
打开文件,打开文件一定要成对写,养成好习惯很重要。比如 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.
<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库函数复习的更多相关文章
- C 标准IO 库函数与Unbuffered IO函数
先来看看C标准I/O库函数是如何用系统调用实现的. fopen(3) 调用open(2)打开指定的文件,返回一个文件描述符(就是一个int 类型的编号),分配一 个FILE 结构体, 通常里面包含了: ...
- linux标准io的copy
---恢复内容开始--- 1.linux标准io的copy #include<stdio.h> int main(int argc,char **argv) { if(argc<3) ...
- UNIX高级环境编程(6)标准IO函数库 - 流的概念和操作
标准IO函数库隐藏了buffer大小和分配的细节,使得我们可以不用关心预分配的内存大小是否正确的问题. 虽然这使得这个函数库很容易用,但是如果我们对函数的原理不熟悉的话,也容易遇到很多问题. 1 ...
- 为什么需要标准IO缓冲?
(转)标准I/O缓冲:全缓冲.行缓冲.无缓冲 标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的 ...
- Linux 标准IO库介绍
1.标准IO和文件IO有什么区别? (1).看起来使用时都是函数,但是:标准IO是C库函数,而文件IO是Linux系统的API. (2).C语言库函数是由API封装而来的.库函数内部也是通过调用API ...
- 深究标准IO的缓存
前言 在最近看了APUE的标准IO部分之后感觉对标准IO的缓存太模糊,没有搞明白,APUE中关于缓存的部分一笔带过,没有深究缓存的实现原理,这样一本被吹上天的书为什么不讲透彻呢?今天早上爬起来赶紧找了 ...
- 标准io与文件io
A: 代码重复: 语句块1: while(判断) { 语句块2: 语句块1: } 上面可以改写为: while(1) { 语句块1: if(判断) break: 语句块2: } B: 标准IO和文件I ...
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
最近学习linux系统下的应用编程,参考书籍是那本称为神书的<Unix环境高级编程>,个人感觉神书不是写给草鞋看的,而是 写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻 ...
- (九)errno和perror、标准IO
3.1.6.文件读写的一些细节3.1.6.1.errno和perror(1)errno就是error number,意思就是错误号码.linux系统中对各种常见错误做了个编号,当函数执行错误时,函数会 ...
随机推荐
- C# 中Newtonsoft.Json的安装和使用
官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令 install-package Newtonsoft.Json 安装Newtonsoft.Js ...
- typedef (还需经常看看加深理解)
看了 c++primer 1,typedef名字 typedef定义以关键字typedef开始,后面是 数据类型+标示符. 并未引入新的类型,只是现有数据类型的同义词 例: typedef doubl ...
- 【poj3208-Apocalypse Someday】数位DP
题意:问你在所有包含666的数中,第n大的是多少.(1 ≤ n ≤ 50,000,000) .开头几个是666, 1666, 2666, 3666, 4666, 5666… 题解: 这题可以用AC自动 ...
- ArchLinux安装与配置小结
最近无意间发现一个基于ArchLinux的发行版--BlackArch,主题十分炫酷(中二).当然渗透类的Linux 发行版已经有BackTrack和Kali了,不过都是源于Debian的,使用者众多 ...
- iOS开发--单例模式
单例模式在iOS开发中可能算是最常用的模式之一了,但是由于oc本身的语言特性,想要写一个正确的单例模式相对来说比较麻烦,这里我就抛砖引玉来聊一聊iOS中单例模式的设计思路.关于单例模式更多的介绍请参考 ...
- 物联网操作系统Hello China V1.76(PC串口版)版本发布
作为向ARM平台移植的基线版本,经过三个多月的努力,Hello China V1.76终于完成并发布.相对原来发布的V1.75版本,该版本主要做了如下修改: 彻底去掉了原来版本源代码中的C++特性,采 ...
- android 矩阵处理类:Matrix
在Android中,对图片的处理需要使用到Matrix类,Matrix是一个3 x 3的矩阵,他对图片的处理分为四个基本类型: 1.Translate 2.Scale 3.Rotate 4.Skew ...
- SIM卡
SIM卡是(Subscriber Identity Module 客户识别模块)的缩写 也称为用户身份识别卡.智能卡,GSM数字移动电话机必须装上此卡方能使用.在电脑芯片上存储了数字移动电话客户的信息 ...
- DXT纹理压缩
转:http://blog.csdn.net/lhc717/article/details/6802951 我们知道游戏中对于3D物体表面细节的表现最重要的还是靠贴图来实现的,那么越是高分辨率越是真彩 ...
- 事务回滚后,自增ID仍然增加
回滚后,自增ID仍然增加. 比如当前ID是7,插入一条数据后,又回滚了.然后你再插入一条数据,此时插入成功,这时候你的ID不是8,而是9.因为虽然你之前插入回滚,但是ID还是自增了. 如果你认为自增I ...