博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c fopen文件读写
阅读量:6976 次
发布时间:2019-06-27

本文共 3534 字,大约阅读时间需要 11 分钟。

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 (, ) 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 () or repositioned (, , ) 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  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 
int main () {
FILE * pFile; pFile = fopen ("myfile.txt","w"); if (pFile!=NULL) {
fputs ("fopen example",pFile); fclose (pFile); } return 0; }
原文连接:
一个不错的C++在线帮助文档 

转载于:https://www.cnblogs.com/08shiyan/archive/2012/03/31/2426479.html

你可能感兴趣的文章
称重管理系统,您的磅房安装了吗
查看>>
11-vue的使用
查看>>
动态规划算法(java)
查看>>
os模块的几种方法
查看>>
tensorflow API _ 4 (Logging with tensorflow)
查看>>
常用模块------时间模块 , random模块 ,os模块 , sys模块
查看>>
10.02 T3 打表找递推式+十进制快速幂 九校联考凉心模拟DAY1T1
查看>>
leetcode — reverse-linked-list
查看>>
linux 命令 — sort、uniq
查看>>
[python]Python代码安全分析工具(Bandit)
查看>>
向继电器发送socket请求(python+java)
查看>>
20165201 2017-2018-2 《Java程序设计》第一周学习总结
查看>>
7. WebDriver API
查看>>
动软.NET代码生成器实例教程使用总结
查看>>
spring框架学习--依赖注入
查看>>
C语言难点4之动态内存分配
查看>>
[模板] 杜教筛 && bzoj3944-Sum
查看>>
第一次作业
查看>>
SpringBoot2.x的Maven依赖配置
查看>>
17.11.16
查看>>