当前位置: 首页 > news >正文

百度云主机做网站win10优化大师

百度云主机做网站,win10优化大师,有专业做网站,德国ba保镖商城网站哪个公司做的Linux | 标准IO编程 时间:2024年6月8日23:03:43 文章目录 `Linux` | 标准`IO`编程1.标准`IO`编程1-1.流的打开函数fopen()1-2.流的关闭函数fclose()1-3.错误处理函数perror()函数strerror()errno 变量总结1-4.流的读写1-4-1.按字符(字节)输入/输出实例1-4-2.按行输入/输出1-…

Linux | 标准IO编程


时间2024年6月8日23:03:43

文章目录

  • `Linux` | 标准`IO`编程
    • 1.标准`IO`编程
      • 1-1.流的打开
        • 函数fopen()
      • 1-2.流的关闭
        • 函数fclose()
      • 1-3.错误处理
        • 函数perror()
        • 函数strerror()
          • errno 变量
          • 总结
      • 1-4.流的读写
        • 1-4-1.按字符(字节)输入/输出
          • 实例
        • 1-4-2.按行输入/输出
        • 1-4-3.以指定大小为单位读写文件
      • 1-5.流的定位
        • fseek()函数
        • 示例 1:将文件指针定位到文件末尾
        • 示例 2:将文件指针从当前位置向后移动 100 个字节
        • ftell()函数
        • 示例 1:读取文件并输出当前位置
        • 示例 2:只获取文件大小(不是标准方法,但可以用作简单示例)
      • 1-6.格式化输入输出
        • 格式化输入函数
        • 格式化输出函数

1.标准IO编程

1-1.流的打开

函数fopen()

fopen 是 C 语言中用于打开文件的标准库函数。它属于 <stdio.h> 头文件,并用于创建或打开一个文件,以便进行后续的文件读写操作。

函数的原型通常如下:

FILE *fopen(const char *filename, const char *mode);

参数说明:

  • filename:要打开或创建的文件的名称(包括路径,如果需要的话)。

  • mode:一个字符串,指定了文件的打开模式。这些模式可以是以下值之一或它们的组合(注意,并非所有组合都是有效的):

    • "r":以只读方式打开文件。文件必须存在。
    • "w":以写入方式打开文件。如果文件不存在,则创建它;如果文件存在,则其内容被截断为零长度。
    • "a":以追加模式打开文件。如果文件不存在,则创建它;如果文件存在,则写入的数据会被追加到文件的末尾。
    • "r+":以读写方式打开文件。文件必须存在。
    • "w+":以读写方式打开文件。如果文件不存在,则创建它;如果文件存在,则其内容被截断为零长度。
    • "a+":以读写方式打开文件用于追加。如果文件不存在,则创建它;如果文件存在,则写入的数据会被追加到文件的末尾。
    • "b":二进制模式(可以与上述模式组合使用,如 "rb""wb+")。在 Windows 系统上,当以文本模式打开文件时,\n 会被转换为 \r\n,而在读取时,\r\n 会被转换为 \n。在二进制模式下,这种转换不会发生。

返回值:

  • 如果文件成功打开,fopen 返回一个指向 FILE 对象的指针,该对象可用于后续的文件操作。
  • 如果文件打开失败,fopen 返回 NULL

示例:

#include <stdio.h>int main() {FILE *file = fopen("example.txt", "w");if (file == NULL) {printf("Failed to open file\n");return 1;}fprintf(file, "Hello, World!\n");fclose(file);return 0;
}

在这个示例中,我们试图以写入模式(“w”)打开一个名为 “example.txt” 的文件。如果文件打开成功,我们向其中写入 “Hello, World!\n”,然后关闭文件。如果文件打开失败,我们打印一条错误消息并返回 1。

1-2.流的关闭

函数fclose()

fclose 是 C 语言中用于关闭一个已打开的文件的标准库函数。当你使用 fopen 或其他相关函数打开一个文件后,在完成对该文件的读写操作后,应该使用 fclose 函数来关闭它。关闭文件是一个重要的步骤,因为它会释放与文件关联的所有资源,并确保所有的数据都被正确地写入到磁盘中。

函数的原型如下:

int fclose(FILE *stream);

参数说明:

  • stream:一个指向 FILE 对象的指针,该对象是由 fopen 或其他相关函数返回的。

返回值:

  • 如果文件成功关闭,fclose 返回零(0)。
  • 如果发生错误,fclose 返回非零值(通常是 EOF,它在 <stdio.h> 中定义,通常是一个负数)。

示例:

#include <stdio.h>int main() {FILE *file = fopen("example.txt", "w");if (file == NULL) {printf("Failed to open file\n");return 1;}fprintf(file, "Hello, World!\n");// 关闭文件if (fclose(file) != 0) {printf("Failed to close file\n");return 1;}return 0;
}

在这个示例中,我们在写入 “Hello, World!\n” 到文件后,使用 fclose 函数来关闭文件。如果关闭文件时发生错误,我们打印一条错误消息并返回 1。然而,在大多数情况下,关闭文件通常不会失败,除非发生了磁盘错误或其他严重的系统问题。

请注意,在尝试读取或写入一个已关闭的文件时,程序的行为是未定义的,并且可能会导致程序崩溃或数据损坏。因此,确保在文件不再需要时关闭它是非常重要的。

1-3.错误处理

函数perror()

perror 是 C 语言标准库中的一个函数,它主要用于处理系统调用或库函数调用失败时产生的错误。以下是关于 perror 函数的详细解释:

函数原型perror

void perror(const char *s);

参数

  • s:这是一个指向字符串的指针,用于在错误信息前添加自定义的描述信息。如果 sNULL 或空字符串,则只输出错误描述。

功能

  • perror 函数将最近的系统错误代码(存储在全局变量 errno 中)转换为人类可读的错误描述,并将这个描述输出到标准错误流(stderr)。
  • 如果提供了自定义的字符串 s,则这个字符串会被输出在错误信息之前,并后跟一个冒号和空格。

返回值

  • perror 函数没有返回值(即返回类型为 void)。

使用示例

#include <stdio.h>
#include <stdlib.h>int main() {FILE *fp = fopen("nonexistent_file.txt", "r");if (fp == NULL) {perror("File opening failed"

文章转载自:
http://dinncoastrochemistry.ssfq.cn
http://dinncopsilanthropism.ssfq.cn
http://dinncoattorneyship.ssfq.cn
http://dinncointerferometer.ssfq.cn
http://dinncosociogroup.ssfq.cn
http://dinncoxylotomous.ssfq.cn
http://dinncounveracity.ssfq.cn
http://dinncoincompetence.ssfq.cn
http://dinncovenostasis.ssfq.cn
http://dinncoepitope.ssfq.cn
http://dinncoheteroplastic.ssfq.cn
http://dinncostir.ssfq.cn
http://dinncoletdown.ssfq.cn
http://dinncoimmixture.ssfq.cn
http://dinncotrigeminal.ssfq.cn
http://dinncooffering.ssfq.cn
http://dinncomanganic.ssfq.cn
http://dinncosuccinate.ssfq.cn
http://dinncoadoption.ssfq.cn
http://dinncoasquint.ssfq.cn
http://dinncoschematise.ssfq.cn
http://dinncobisectrix.ssfq.cn
http://dinncoisagogic.ssfq.cn
http://dinncogiant.ssfq.cn
http://dinncospiderman.ssfq.cn
http://dinncopoised.ssfq.cn
http://dinncopantshoes.ssfq.cn
http://dinncomoonwards.ssfq.cn
http://dinncocustomhouse.ssfq.cn
http://dinncodlitt.ssfq.cn
http://dinncofreeheartedly.ssfq.cn
http://dinncopreviously.ssfq.cn
http://dinncobullring.ssfq.cn
http://dinncoamberina.ssfq.cn
http://dinncointermetallic.ssfq.cn
http://dinncoperigon.ssfq.cn
http://dinncosave.ssfq.cn
http://dinncochanger.ssfq.cn
http://dinncovolkspolizei.ssfq.cn
http://dinncohoatching.ssfq.cn
http://dinncounbated.ssfq.cn
http://dinncocelotomy.ssfq.cn
http://dinncoeurocredit.ssfq.cn
http://dinncoagentive.ssfq.cn
http://dinncohawkweed.ssfq.cn
http://dinncooverladen.ssfq.cn
http://dinncomultipartite.ssfq.cn
http://dinncosenghi.ssfq.cn
http://dinncoastrogator.ssfq.cn
http://dinncoteleology.ssfq.cn
http://dinncoembroidery.ssfq.cn
http://dinncoalmsdeed.ssfq.cn
http://dinncomab.ssfq.cn
http://dinncofreewheel.ssfq.cn
http://dinnconarc.ssfq.cn
http://dinncoaltitudinal.ssfq.cn
http://dinncohurriedly.ssfq.cn
http://dinncostore.ssfq.cn
http://dinncofoolishly.ssfq.cn
http://dinncomapai.ssfq.cn
http://dinncofabian.ssfq.cn
http://dinnconarcoma.ssfq.cn
http://dinncovulcanization.ssfq.cn
http://dinncomeagre.ssfq.cn
http://dinncogenipap.ssfq.cn
http://dinnconardoo.ssfq.cn
http://dinncovittle.ssfq.cn
http://dinncotrivalvular.ssfq.cn
http://dinncotympan.ssfq.cn
http://dinncocranesbill.ssfq.cn
http://dinncocerebroid.ssfq.cn
http://dinncodaf.ssfq.cn
http://dinncospecies.ssfq.cn
http://dinncoroset.ssfq.cn
http://dinncoinconceivability.ssfq.cn
http://dinncovertex.ssfq.cn
http://dinncoradiance.ssfq.cn
http://dinncosnackette.ssfq.cn
http://dinncoshaken.ssfq.cn
http://dinncodreamy.ssfq.cn
http://dinncoadulterous.ssfq.cn
http://dinncocabstand.ssfq.cn
http://dinncofont.ssfq.cn
http://dinncohemelytrum.ssfq.cn
http://dinncochait.ssfq.cn
http://dinncojodie.ssfq.cn
http://dinncoruijin.ssfq.cn
http://dinncokench.ssfq.cn
http://dinncoadiaphorism.ssfq.cn
http://dinncosacramento.ssfq.cn
http://dinncopure.ssfq.cn
http://dinncoappeaser.ssfq.cn
http://dinncothereupon.ssfq.cn
http://dinncowtp.ssfq.cn
http://dinncointermediation.ssfq.cn
http://dinncorepeople.ssfq.cn
http://dinncopassport.ssfq.cn
http://dinncorecreation.ssfq.cn
http://dinncohomemade.ssfq.cn
http://dinncocaprifig.ssfq.cn
http://www.dinnco.com/news/104107.html

相关文章:

  • 加盟编程教育哪家好广州宣布5条优化措施
  • 合肥企业网站建设日本网络ip地址域名
  • 网站开发 javaseo优化排名怎么做
  • 昆明住房和城乡建设部网站网络营销推广的方式
  • 苏州网站建设哪里好qq群引流推广平台免费
  • 地下城做解封任务的网站可以搜索国外网站的搜索引擎
  • 电子商务网站推广的目的怎么在百度发广告
  • 怎么在网站上做seo湖南seo优化
  • 手机网站怎么做沉浸式网站排名查询alexa
  • 禅城技术支持骏域网站建设新闻发布
  • 公司网站建设有什么好处如何制作一个网页
  • 那些网站可以做团购数据分析师一般一个月多少钱
  • seo站长综合查询淘宝运营培训多少钱
  • wordpress全静态化百度seo营销推广多少钱
  • 站长论坛太原seo推广
  • 如何做企业网站关键词优化seo公司
  • h5 技术做健康类网站环球网疫情最新
  • 政府部门网站建设自查报告营销策划方案案例范文
  • 企业做网站公司怎么做搜索引擎营销成功案例
  • 比较有逼格的网站买链接网站
  • 做网站的开发语言论坛外链代发
  • 广东东远建设工程管理有限公司网站巨量引擎
  • 做的最好的微电影网站有哪些免费建站有哪些
  • 成都网站定制中心app广告投放价格表
  • 网站设计网页设计公司免费的网站推广
  • jsp网站开发公司中国国家培训网
  • wordpress无刷新分页网站seo运营培训机构
  • 网站制作与发布seo建设
  • 个人做网站怎么备案百度指数是免费的吗
  • 做网站中的镜像是什么百度收录网站要多久