c fopen文件读写

简介:

fopen

<cstdio>
FILE * fopen ( const char * filename, const char * mode );
Open file

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 (fseekrewind) 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 (fseekfsetposrewind) 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

1
2
3
4
5
6
7
8
9
10
11
12
13
/* fopen example */ #include <stdio.h> int main () 
{
FILE * pFile; pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
}

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' 标记。



本文转自莫水千流博客园博客,原文链接:http://www.cnblogs.com/zhoug2020/p/5851733.html,如需转载请自行联系原作者

相关文章
|
存储 测试技术 编译器
文件操作【fopen/fclose/fputs/fgets】【C语言/进阶】
文件操作【fopen/fclose/fputs/fgets】【C语言/进阶】
121 0
|
3月前
|
安全 C# 开发者
C# 一分钟浅谈:文件操作与文件流详解
【9月更文挑战第4天】在日常开发中,文件的读写是基本而重要的任务。C# 通过 `System.IO` 命名空间提供了多种工具,如 `FileStream`、`StreamReader` 和 `StreamWriter` 等,用于处理文件和流。本文从基础概念入手,详细介绍了这些类的使用方法,并讨论了常见错误及其避免策略,包括文件不存在、权限问题和文件被占用等。通过示例代码,展示了如何创建、读取文件以及进行二进制数据操作,并强调了异常处理和性能优化的重要性。掌握这些技巧对于提升编程能力至关重要。
204 2
|
存储 iOS开发 C++
C++中文件操作与文件流
🐰文件操作与文件流 🏡文件流类和文件流对象 🏡文件的打开与关闭 🌸1.文件的打开 🌸2.文件的关闭 🏡对文本文件的操作 🏡对二进制文件的操作 🌸1.用成员函数write和read操作二进制文件 🌸2.随机访问二进制文件
文件操作以及相关的函数fwrite,fread,fseek,ftell,rwind,feof
🐰文件操作 🌸 fwrite 🌸fread 🌸fseek 🌸fteel 🌸rwind 🌸文本文件和二进制文件 🌸文件结束的判定 🌸文件缓冲区 🌸 实现拷贝一个文件
其他文件操作
其他文件操作
99 0
|
C语言
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
483 0
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
|
移动开发 Unix Linux
【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
227 0
 【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
|
C语言
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
345 0
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
|
C语言
【C 语言】文件操作 (fscanf、fprintf 函数)(一)
【C 语言】文件操作 (fscanf、fprintf 函数)(一)
156 0
【C 语言】文件操作 (fscanf、fprintf 函数)(一)