c fopen文件读写
fopen
FILE * fopen ( const char * filename, const char * mode );
Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter.
The running environment supports at least FOPEN_MAX files open simultaneously; FOPEN_MAX is a macro constant defined in <cstdio>.
Parameters
- filename
- C string containing the name of the file to be
opened. This paramenter must follow the file name specifications of the
running environment and can include a path if the system supports it. - mode
- C string containing a file access modes. It can be:
"r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name
already exists its content is erased and the file is treated as a new
empty file."a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with
the same name already exists its content is erased and the file is
treated as a new empty file."a+" Open a file for reading and appending. All writing operations are
performed at the end of the file, protecting the previous content to be
overwritten. You can reposition (fseek, rewind)
the internal pointer to anywhere in the file for reading, but writing
operations will move it back to the end of file. The file is created if
it does not exist.
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character
can either be appended at the end of the string (thus making the
following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be
inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
In the case of text files,
depending on the environment where the application runs, some special
character conversion may occur in input/output operations to adapt them
to a system-specific text file format. In many environments, such as
most UNIX-based systems, it makes no difference to open a file as a text
file or a binary file; Both are treated exactly the same way, but
differentiation is recommended for a better portability.
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
Return Value
If the file has been successfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.
Example
|
|
fopen ( string filename, string mode )
返回值是 FILE*
fopen() 中的 mode 的可能值列表
| mode | 说明 |
|---|---|
| 'r' | 只读方式打开,将文件指针指向文件头。 |
| 'r+' | 读写方式打开,将文件指针指向文件头。 |
| 'w' | 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
| 'w+' | 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
| 'a' | 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
| 'a+' | 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
| 'x' | 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。 |
| 'x+' | 创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。 |
在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。
为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。
c fopen文件读写的更多相关文章
- (转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/ ...
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
由于最近经常使用到c语言中的读写文件,所以在此总结以下,方便以后查找. 在c中,文件操作都是由库函数来实现的,主要是分为读和写两种操作,以下详细讲解以下所有有关文件操作的邯郸乎的用法: //C++写入 ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- Python之文件读写
本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...
- PHP文件读写操作之文件写入代码
在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...
- Linux高级编程--05.文件读写
缓冲I/O和非缓冲I/O 文件读写主要牵涉到了如下五个操作:打开.关闭.读.写.定位.在Linux系统中,提供了两套API, 一套是C标准API:fopen.fclose.fread.fwrite.f ...
- C之文件读写
1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此 ...
- 通过文件读写方式实现Matlab和Modelsim的联合仿真
虽然Modelsim的功能非常强大,仿真的波形可以以多种形式进行显示,但是当涉及到数字信号处理的算法的仿真验证的时候,则显得有点不足.而进行数字信号处理是Matlab的强项,不但有大量的关于数字信号处 ...
- 【转】文件读写NDK(或Linux)
原文网址:http://www.ithao123.cn/content-10709539.html 使用NDK进行文件读写,有利于保存数据的安全性,项目需要,要文件读写从Java中处理搬到Linux平 ...
随机推荐
- makefile--模式规则(七)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节讲到目录创建成功,目标文件没有生产到对应目录下,这里我们先给目标文件加上对应目录,这样的话 ...
- PHP部分---字符串处理(文中例子不断更新);
字符串处理: 1.去除 ltrim(),rtrim()函数 trim()函数 trim(字符串名)函数:--去掉字符串两边的空格 ltrim(字符串名,“特殊字符”);--去除字符串左边的空格和 ...
- js部分---表单验证;(含正则表达式)
1.非空验证 <div>用户名:<input id="yhm" type="text" name="yhm" />& ...
- C#部分---利用arraylist集合做滚动抽奖;
输入多个手机号码,放到集合中,进行三秒钟的滚动抽奖:随机显示号码,清空,再显示: 1.收集号码: 2.每隔三秒进行抽奖,及作弊代码,哈哈哈: 3.System.Threading.Thread.Sle ...
- 國王遊戲(2012年NOIP全国联赛提高组)
题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位大臣排成 ...
- ExtJS常用的正则表达式
正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. Ext.onReady(function(){ Ext.QuickTips.init(); }); E ...
- [BeiJing2010组队][BZOJ 1977]次小生成树 Tree
话说这个[BeiJing2010组队]是个什喵玩意? 这是一道严格次小生成树,而次小生成树的做法是层出不穷的 MATO IS NO.1 的博客里对两种算法都有很好的解释,值得拥有: (果然除我以外, ...
- ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)
问题描述:一个在Django框架下使用Python编写的定时更新项目,在一个Linux系统下运行没有问题,在另外一台Linux系统下测试,报如下错误: ascii codec can't decode ...
- Linux-以指定用户运行redis
redis中无配置启动用户信息,需要添加redis用户,后以其启动 useradd -s /bash/false -M redis >& &
- 大白话系列之C#委托与事件讲解(二)
什么是事件?EVENT?点击事件?加载事件?一连串的模糊的概念冲击着我们弱小的脑袋 那我们首先来看一下比较正统的感念吧: 事件是类在发生其关注的事情时用来提供通知的一种方式. 事件的发生一般都牵扯2个 ...