fopen函数的用法示例

In C and C++ programming languages fopen() function is used to open files and make operations like add, update, create for data. In this tutorial we will learn the syntax, usage and errors about the fopen() function.

在C和C ++编程语言中, fopen()函数用于打开文件并进行诸如添加,更新和创建数据之类的操作。 在本教程中,我们将学习有关fopen()函数的语法,用法和错误。

fopen()函数语法 (fopen() Function Syntax)

fopen() function is provided by the standard C library. The fopen() function has the following syntax.

fopen()函数由标准C库提供。 fopen()函数具有以下语法。

FILE *fopen(const char *FILENAME, const char *MODE)
  • `FILE *` is the return type of the fopen() function which is a pointer to a FILE structure. This file pointer is used for operations to add, update, etc.FILE *是fopen()函数的返回类型,它是指向FILE结构的指针。 该文件指针用于添加,更新等操作。
  • `const char *FILENAME` is simply the file name that will be opened by fopen() function.`const char * FILENAME`只是将由fopen()函数打开的文件名。
  • `const char *MODE` is the file open mode that will set the behavior of the file operations like only read, write, append, etc.const char * MODE是文件打开模式,它将设置文件操作的行为,例如仅读,写,追加等。

The fopen() function will return a FILE pointer to the opened file in successful execution. If there is error the return value will be NULL and the global variable errno will be set for the related error.

fopen()函数将在成功执行后返回指向已打开文件的FILE指针。 如果存在错误,则返回值将为NULL,并且将为相关错误设置全局变量errno

文件打开模式 (File Open Modes)

Before starting the examples of the fopen() function we will learn file open modes. File open modes sets and restrict file access type like only read, update, create the file if do not exists, etc. File modes are represented as characters and + sign.

在开始fopen()函数的示例之前,我们将学习文件打开模式。 文件打开模式设置和限制文件访问类型,例如仅读取,更新,创建文件(如果不存在)等。文件模式表示为字符和+号。

  • `”r”` is used as the file for reading. This will open the file as read-only and the file cannot be edited in this mode. As expected the file should exist where it will not be created automatically.“ r”用作读取文件。 这将以只读方式打开文件,并且在此模式下无法编辑文件。 如预期的那样,该文件应存在于不会自动创建的位置。
  • `”w”` will create an empty file for writing. If the file already exists the existing file will be deleted or erased and the new empty file will be used. Be cautious while using these options.“” w”将创建一个用于写入的空文件。 如果文件已存在,则将删除或删除现有文件,并使用新的空文件。 使用这些选项时要小心。
  • `”a”` is used for appending new data into the specified file. The file will be created if it doesn’t exist.“ a”用于将新数据附加到指定文件中。 如果该文件不存在,则将创建该文件。
  • `”r+”` mode will open the file to update which will provide read and write rights. But the file must already exist if not it will not be created and throw an error.“ r +”模式将打开要更新的文件,该文件将提供读写权限。 但是文件必须已经存在,否则将无法创建并抛出错误。
  • `”w+”` will create an empty file for both reading and writing.“” w +”`将创建一个用于读取和写入的空文件。
  • `”a+”` will open a file for reading and appending.“” a +”`将打开一个文件以供读取和附加。
.uc2db8be092464257f9c0abf57bb61e90 , .uc2db8be092464257f9c0abf57bb61e90 .postImageUrl , .uc2db8be092464257f9c0abf57bb61e90 .centered-text-area { min-height: 80px; position: relative; } .uc2db8be092464257f9c0abf57bb61e90 , .uc2db8be092464257f9c0abf57bb61e90:hover , .uc2db8be092464257f9c0abf57bb61e90:visited , .uc2db8be092464257f9c0abf57bb61e90:active { border:0!important; } .uc2db8be092464257f9c0abf57bb61e90 .clearfix:after { content: ""; display: table; clear: both; } .uc2db8be092464257f9c0abf57bb61e90 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .uc2db8be092464257f9c0abf57bb61e90:active , .uc2db8be092464257f9c0abf57bb61e90:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .uc2db8be092464257f9c0abf57bb61e90 .centered-text-area { width: 100%; position: relative; } .uc2db8be092464257f9c0abf57bb61e90 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .uc2db8be092464257f9c0abf57bb61e90 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .uc2db8be092464257f9c0abf57bb61e90 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .uc2db8be092464257f9c0abf57bb61e90:hover .ctaButton { background-color: #E67E22!important; } .uc2db8be092464257f9c0abf57bb61e90 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .uc2db8be092464257f9c0abf57bb61e90 .uc2db8be092464257f9c0abf57bb61e90-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .uc2db8be092464257f9c0abf57bb61e90:after { content: ""; display: block; clear: both; }

LEARN MORE  What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java

.uc2db8be092464257f9c0abf57bb61e90 , .uc2db8be092464257f9c0abf57bb61e90 .postImageUrl , .uc2db8be092464257f9c0abf57bb61e90 .centered-text-area { min-height: 80px; position: relative; } .uc2db8be092464257f9c0abf57bb61e90 , .uc2db8be092464257f9c0abf57bb61e90:hover , .uc2db8be092464257f9c0abf57bb61e90:visited , .uc2db8be092464257f9c0abf57bb61e90:active { border:0!important; } .uc2db8be092464257f9c0abf57bb61e90 .clearfix:after { content: ""; display: table; clear: both; } .uc2db8be092464257f9c0abf57bb61e90 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .uc2db8be092464257f9c0abf57bb61e90:active , .uc2db8be092464257f9c0abf57bb61e90:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .uc2db8be092464257f9c0abf57bb61e90 .centered-text-area { width: 100%; position: relative; } .uc2db8be092464257f9c0abf57bb61e90 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .uc2db8be092464257f9c0abf57bb61e90 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .uc2db8be092464257f9c0abf57bb61e90 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .uc2db8be092464257f9c0abf57bb61e90:hover .ctaButton { background-color: #E67E22!important; } .uc2db8be092464257f9c0abf57bb61e90 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .uc2db8be092464257f9c0abf57bb61e90 .uc2db8be092464257f9c0abf57bb61e90-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .uc2db8be092464257f9c0abf57bb61e90:after { content: ""; display: block; clear: both; }

了解更多什么是EOF(文件末尾)? PHP,C ++,C,Python,Java的示例

打开文件进行读取(Open File For Reading)

We will start with a simple example where we will open a file named myfile.txt which is a text file as you expect. But keep in mind that the type or content of the file is not important for fopen() function.

我们将从一个简单的示例开始,我们将打开一个名为myfile.txt的文件,该文件是您所期望的文本文件。 但是请记住,文件的类型或内容对于fopen()函数并不重要。

#include <stdio.h>int main () {//File pointer for "myfile.txt"FILE *fp;int c;//Open the file and set to pointer fpfp = fopen("myfile.txt","r");//Read file character by character and //put to the standard ouputwhile(1) {c = fgetc(fp);if( feof(fp) ) {break ;}printf("%c", c);}//Close the file pointerfclose(fp);return(0);
}

We will compile the source code file read_myfile.c with the following gcccommand.

我们将使用以下gcc命令编译源代码文件read_myfile.c

$ gcc read_myfile.c -o read_myfile

打开文件进行写入 (Open File For Writing)

We can also use "a" for writing or appending into the existing file. In this example, we will add the following line file named myfile.txt.

我们还可以使用"a"将其写入或追加到现有文件中。 在此示例中,我们将添加以下名为myfile.txt行文件。

I am new line
#include <stdio.h>int main () {//File pointer for "myfile.txt"FILE *fp;int c;//Open the file and set to pointer fpfp = fopen("myfile.txt","a");//Add or append new line to myfile.txt// by using fp pointerfputs("I am a new line",fp);//Close the file pointerfclose(fp);return(0);
}

关闭档案 (Close File)

As fopen() function is used to open a file after the operations are completed we have to close the file. fclose() function is used to close the given file by using this file pointer and release resources. In previous examples as the last action, we closed the files like below.

由于fopen()函数用于在操作完成后打开文件,因此我们必须关闭文件。 fclose()函数用于通过使用此文件指针关闭给定文件并释放资源。 在前面的示例中,作为最后一个操作,我们关闭了如下文件。

fclose(fp);

fopen()错误 (fopen() Errors)

While opening files with fopen() function we may get different types of errors. Here we will provide some of the most possible ones.

使用fopen()函数打开文件时,我们可能会遇到不同类型的错误。 在这里,我们将提供一些最可能的方法。

  • `EACCES` is related where there is no permission for the specified operation.如果没有指定操作的许可,则“ EACCES”相关。
  • `EINVAL` will raise when the value of the mode argument is not valid.当mode参数的值无效时,ʻEINVAL`将升高。
  • `EINTR` will raise when a signal was caught during fopen().在fopen()期间捕获到信号时,ʻEINTR`将升高。
.udf88f3ea9d39640e37a892e7d80dff99 , .udf88f3ea9d39640e37a892e7d80dff99 .postImageUrl , .udf88f3ea9d39640e37a892e7d80dff99 .centered-text-area { min-height: 80px; position: relative; } .udf88f3ea9d39640e37a892e7d80dff99 , .udf88f3ea9d39640e37a892e7d80dff99:hover , .udf88f3ea9d39640e37a892e7d80dff99:visited , .udf88f3ea9d39640e37a892e7d80dff99:active { border:0!important; } .udf88f3ea9d39640e37a892e7d80dff99 .clearfix:after { content: ""; display: table; clear: both; } .udf88f3ea9d39640e37a892e7d80dff99 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .udf88f3ea9d39640e37a892e7d80dff99:active , .udf88f3ea9d39640e37a892e7d80dff99:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .udf88f3ea9d39640e37a892e7d80dff99 .centered-text-area { width: 100%; position: relative; } .udf88f3ea9d39640e37a892e7d80dff99 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .udf88f3ea9d39640e37a892e7d80dff99 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .udf88f3ea9d39640e37a892e7d80dff99 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .udf88f3ea9d39640e37a892e7d80dff99:hover .ctaButton { background-color: #E67E22!important; } .udf88f3ea9d39640e37a892e7d80dff99 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .udf88f3ea9d39640e37a892e7d80dff99 .udf88f3ea9d39640e37a892e7d80dff99-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .udf88f3ea9d39640e37a892e7d80dff99:after { content: ""; display: block; clear: both; }

LEARN MORE  What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java

.udf88f3ea9d39640e37a892e7d80dff99 , .udf88f3ea9d39640e37a892e7d80dff99 .postImageUrl , .udf88f3ea9d39640e37a892e7d80dff99 .centered-text-area { min-height: 80px; position: relative; } .udf88f3ea9d39640e37a892e7d80dff99 , .udf88f3ea9d39640e37a892e7d80dff99:hover , .udf88f3ea9d39640e37a892e7d80dff99:visited , .udf88f3ea9d39640e37a892e7d80dff99:active { border:0!important; } .udf88f3ea9d39640e37a892e7d80dff99 .clearfix:after { content: ""; display: table; clear: both; } .udf88f3ea9d39640e37a892e7d80dff99 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .udf88f3ea9d39640e37a892e7d80dff99:active , .udf88f3ea9d39640e37a892e7d80dff99:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .udf88f3ea9d39640e37a892e7d80dff99 .centered-text-area { width: 100%; position: relative; } .udf88f3ea9d39640e37a892e7d80dff99 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .udf88f3ea9d39640e37a892e7d80dff99 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .udf88f3ea9d39640e37a892e7d80dff99 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .udf88f3ea9d39640e37a892e7d80dff99:hover .ctaButton { background-color: #E67E22!important; } .udf88f3ea9d39640e37a892e7d80dff99 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .udf88f3ea9d39640e37a892e7d80dff99 .udf88f3ea9d39640e37a892e7d80dff99-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .udf88f3ea9d39640e37a892e7d80dff99:after { content: ""; display: block; clear: both; }

了解更多什么是EOF(文件末尾)? PHP,C ++,C,Python,Java的示例

翻译自: https://www.poftut.com/fopen-function-usage-in-c-and-cpp-with-examples/

fopen函数的用法示例

fopen函数的用法示例_在C和C ++中使用fopen()函数的用法以及示例相关推荐

  1. 课后习题5.13 编写一程序,将两个字符串连接起来,结果取代第一个字符串。 (1)用字符数组,不用stract函数(即自己写一个具有stract函数功能的函数); (2)用标准库中的stract函数;

    课后习题5.13 编写一程序,将两个字符串连接起来,结果取代第一个字符串. (1)用字符数组,不用stract函数(即自己写一个具有stract函数功能的函数): (2)用标准库中的stract函数: ...

  2. python 匿名函数示例_扣丁学堂Python3开发之匿名函数用法示例详解

    扣丁学堂Python3开发之匿名函数用法示例详解 2018-07-26 14:01:11 1324浏览 今天扣丁学堂Python培训给大家分享关于Python3匿名函数用法,结合实例形式分析了Pyth ...

  3. python中imread用法_【转载】Python 中各种imread函数的区别与联系

    Python 中各种imread函数的区别与联系 最近一直在用python做图像处理相关的东西,被各种imread函数搞得很头疼,因此今天决定将这些imread总结一下,以免以后因此犯些愚蠢的错误.如 ...

  4. jQuery动画高级用法(上)——详解animation中的.queue()函数

    如果你拿着一个疑问去找专业人士寻找答案,那么你的一个疑问会变成三个,因为他会用另外两个令你更加一头雾水的名词来解释你的这个疑问. 我想这是大多数,包括我在内,IT人在学习过程中碰到的最大问题.当你有一 ...

  5. hive获取月份_【Hive】Hive中常用日期函数整理

    前言 使用hive进行时间处理的过程中,有时忘记一些常用的函数用法,现整理一下hive中常用日期函数,当做备忘录使用,忘记来翻阅一下. 1 时间戳转化日期函数 时间戳必须是10位,默认情况下转化为'y ...

  6. codeblocks printf函数打印不出来_最全C语言基本程序交互函数之输出到屏幕

    前言 上一栏目主要讲解了各种数据类型的知识,大家先闭眼回顾一下数据类型的知识哦.本章节主要内容是讲解程序的基本交互设计之程序的输出.程序和人交互无非就是通过外设进行输入信息,C语言中基本的交互的基本流 ...

  7. python class用法理解_通过钢铁侠变身快速理解Python的装饰器用法

    1 一切都要从函数说起 我们都知道一个函数可以返回一些数据,然后这些数据可以被其他函数调用.函数里还可以有若干个参数,可以让函数根据不同的输入值进行不同的计算,然后得到新的结果. 于是,我们的故事就可 ...

  8. mysql trunc函数_教您使用SQL中的TRUNC函数

    SQL函数是每个SQL数据库使用者都必须要掌握的,下面就将为您介绍SQL中TRUNC函数的两种用法,供您参考,希望对您学习SQL函数的使用能有帮助. 1.TRUNC(for dates) TRUNC函 ...

  9. pythonproperty装饰器_实例讲解Python编程中@property装饰器的用法

    取值和赋值 class Actress(): def __init__(self): self.name = 'TianXin' self.age = 5 类Actress中有两个成员变量name和a ...

最新文章

  1. 【阿里云 Linux 服务器】购买 Linux 到项目部署过程中遇到的问题,部署 SpringBoot 项目到服务器上,在手机上安装 Android 程序进行测试
  2. 前端学习(1542):cdn简介
  3. Java形参的改变不会影响实参
  4. asp/php招聘,招聘ASP与PHP相关岗位的笔经
  5. 【转】VB代码VB小程序:宇宙大爆炸演示
  6. 直接从Windows7RC版升级安装RTM版本的小窍门
  7. 远离故障的十大原则-好文
  8. GNS3新手安装教程
  9. Exynos 4412处理器IIC总线控制器(包括协议)
  10. 防止按钮在短时间内被多次点击多次触发事件
  11. 图像抓拍录像视频捕获软件微软Amcap怎么使用
  12. OSChina 周六乱弹 —— 感谢女装红薯开办了这个网站
  13. 程序员入职蚂蚁金服第一天就想离职,这并不是个例!
  14. 电子学会2021年6月青少年软件编程(图形化)等级考试试卷(二级)答案解析
  15. Ajax(jQuery封装),表单form提交(Ajax),art-template模板引擎,原生Ajax,XML和JSON,axios,跨域和JSONP,防抖和节流,HTTP协议
  16. Fastly释Lucet原生WebAssembly编译程序和Runtime
  17. 本地blast~TBtools
  18. 分享50个漂亮的设计师个人作品集网站案例
  19. win ce系统如何知道u盘正版授权_从零开始的电脑系统重装
  20. pandas指定从第一行读取正文数据

热门文章

  1. 三人抢答器-20151105
  2. 加米谷大数据干货:Kafka如何实现每秒上百万的超高并发写入?
  3. 成功的背后!(给所有IT人)(转)
  4. 如何设置网页页面高度100%
  5. 不同型号的二极管模块并联_2.5 变频器的并联
  6. JQuery EasyUI后台UI框架使用连载
  7. 源代码加密软件类比图
  8. c语言实现十进制转换为二进制
  9. clion windows 找不到lib库问题
  10. Android 框架学习5:微信热修复框架 Tinker 从使用到 patch 加载、生成、合成原理分析