fopen fdopen freopen 的区别
2007-01-31 14:57
#include <stdio.h>
r 可以 把 字母 ``b'' 添加到 字符串 mode 的 末尾, 或者 插到 上面 任何 两个字符的 字符串 的 中间. 这样 只是 为了 和 ANSI X3.159-1989 (``ANSI C'') 标准 严格 保持 兼容, 没有 实际的 效果; 在 所有遵循 POSIX 的 系统 中, ``b'' 都 被忽略, 包括 Linux. (其他系统 可能会 分别对待 文本文件 和 二进制文件, 如果 在 进行 二进制文件 的 I/O, 那么 添加 ``b'' 是个 好主意, 因为 你的程序 可能会 被移植到 非 Unix 环境中.) 任何 新建文件 的 访问模式 是 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), 并且 用 进程的 掩码值 umask 加以修改 (参见 umask(2)). 在 读/写流 上 可以 任意 顺序 混合 进行 读写操作. 注意 ANSI C 要求 在 输出和输入操作 之间 插入 一个 文件定位函数, 除非 输入操作遇到了 文件结束符. (如果 不是 这种情况, 那么 读操作 总是 返回 写操作 的 结果而不是 最后的 内容.) 因此, 最好 (在 Linux 中 有时 是必须的) 对 这样的流 的 写/读操作 之间 加入 一个 fseek 或是 fgetpos 操作. 这个操作 可以 是一个 空操作 (如 fseek(..., 0L, SEEK_CUR)), 只利用其 文件同步 这个 副效应. 用 追加方式 打开 文件 (a 作为 mode 的 第一个 字符) 将使得 所有后续的 写操作 发生在 文件末尾, 如同 之前 调用了 fseek(stream,0,SEEK_END); freopen 函数 打开 用 path 说明 的 文件, 并且 和 stream 指定的流 联系 起来. 原来的流 (如果 存在的话) 被关闭. 参数 mode 和 fopen 中的 用法 一样. freopen 函数 主要的用处 是 改变 标准文本流 (stderr, stdin, 或 stdout) 联系 的 文件.
ERRORS fopen 也有可能 失败 并置 errno 为 open(2) 指定的值. fdopen 也有可能 失败 并置 errno 为 fcntl(2) 指定的值. freopen 也有可能 失败 并置 errno 为 open(2), fclose(3) 和 fflush(3) 指定的值.
|
fread
目录
展开
编辑本段C语言库函数名:
fread
功 能: 从一个流中读数据
函数原型: int fread(void *ptr, int size, int nitems, FILE *stream);
参 数:用于接收数据的地址(指针)(ptr)
单个元素的大小(size) :单位是字节而不是位,例如读取一个整数值就是4
元素个数(nitems)
提供数据的文件指针(stream)
返回值:成功读取的元素个数
c/c++重定向
这个对经常在OJ上做题的童鞋们很有用。OJ基本都是用标准输入输出(USACO除外)。但如果你在调试的时候也都是从控制台输入,那就太浪费宝贵的时间了。我们可以重定向标准输入,调试的时候从文件读,提交时从标准输入读。
在C语言中,方法比较简单。使用函数freopen():
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
这样就把标准输入重定向到了data.in文件,标准输出重定向到了data.out文件。
这两句代码之后,scanf函数就会从data.in文件里读,而printf函数就会输出到data.out文件里了。
C++中,对流重定向有两个重载函数:
streambuf* rdbuf () const;
streambuf* rdbuf (streambuf*)
就相当于get/set方法。
code
streambuf *backup;
ifstream fin;
fin.open("data.in");
backup = cin.rdbuf(); // back up cin's streambuf
cin.rdbuf(fin.rdbuf()); // assign file's streambuf to cin
// ... cin will read from file
cin.rdbuf(backup); // restore cin's original streambuf
注意最后我们使用了cin.rdbuf(backup)把cin又重定向回了控制台
然而,如果用C语言实现同样的功能就不那么优雅了。
因为标准控制台设备文件的名字是与操作系统相关的。
在Dos/Windows中,名字是con
freopen("con", "r", stdin);
在Linux中,控制台设备是/dev/console
freopen("/dev/console", "r", stdin);
另外,在类unix系统中,也可以使用dup系统调用来预先复制一份原始的stdin句柄。
http://whlforeverwhl.blog.163.com/blog/static/4362096720108821513850/
有关fstream中openmode讨论
c++和linux 2008-12-15 19:40:26 阅读377 评论0 字号:大中小 订阅
今天很恼火,是因为自己了,和上一次犯了一个同样的错误,分析出了原因但是并没有把错误记录下来,今天在这里想好好的分析一下fstream中openmode的问题,在msdn中介绍了有六种打开的方式,第一种为app我认为是单词(append)意思,感觉的英文的解释就是在在插入之前先把光标定位到文件的末尾。第二种为ate,英文的解释为to seek to the end of a stream when its controlling object is first created。第三种为binary,英文的解释为以二进制的方式去读文件,而不是以字节方式去读文件。第四种就是in从文件中读取数据。第五种就是out向文件中写入数据。第六种是trunc, to delete contents of an existing file when its controlling object is created。
eg:#include <iostream>
#include <fstream>
int main ( )
{
using namespace std;
fstream file;
file.open( "rm.txt", ios_base::out | ios_base::trunc );
file << "testing";
}
一下就是最好的东东了,是在msdn中找到的,是fstream::open(filename,strmode)有关几种方式的组合讨论问题,是英文的,我感觉学计算机还是多看英文的,就不再翻译了:
The member function opens the file with filename filename, by calling fopen(filename, strmode). strmode is determined from mode &
~(ate & | binary):
- ios_base::in becomes "r" (open existing file for reading).
- ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
- ios_base::out | app becomes "a" (open existing file for appending all writes).
- ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
- ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
- ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).
If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end of file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.