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

工商网站wordpress获取热门文章

工商网站,wordpress获取热门文章,恒大地产最新消息,济南seo排行榜c 写文件 文章目录 c 写文件1️⃣ 使用 ofstream 写入文本文件2️⃣ 追加模式写入3️⃣ 写入二进制文件4️⃣ 使用 fstream 进行读写5️⃣ 使用 fprintf()(C 方式)6️⃣ 使用 write() 低级 I/O 方式推荐方式 C 写文件的几种方式主要有以下几种&#xff1…

c++ 写文件

文章目录

  • c++ 写文件
    • 1️⃣ 使用 `ofstream` 写入文本文件
    • 2️⃣ 追加模式写入
    • 3️⃣ 写入二进制文件
    • 4️⃣ 使用 `fstream` 进行读写
    • 5️⃣ 使用 `fprintf()`(C 方式)
    • 6️⃣ 使用 `write()` 低级 I/O 方式
    • 推荐方式

C++ 写文件的几种方式主要有以下几种:

方式适用场景示例
std::ofstream文本写入file << "text"
std::ofstream (std::ios::app)追加文本file << "text"
std::ofstream (std::ios::binary)二进制写入file.write(data, size)
std::fstream读写模式file << "text"
fprintf() (C方式)C风格写入fprintf(file, "text")
write() (低级I/O)高效文件操作write(fd, data, size)

1️⃣ 使用 ofstream 写入文本文件

最常见的方法,适用于写入纯文本数据。

#include <iostream>
#include <fstream>int main() {std::ofstream file("output.txt");  // 打开文件进行写入(默认会覆盖原文件)if (!file) {std::cerr << "无法打开文件!" << std::endl;return 1;}file << "Hello, World!" << std::endl;  // 写入文本file.close();  // 关闭文件return 0;
}

🔹 说明

  • std::ofstream file("output.txt"); 打开文件,默认覆盖原文件内容。
  • file << "Hello, World!" << std::endl;流式 方式写入数据。

2️⃣ 追加模式写入

在文件末尾追加内容,不覆盖原有内容,使用 std::ios::app

#include <fstream>int main() {std::ofstream file("output.txt", std::ios::app);  // 追加模式if (file) {file << "追加内容..." << std::endl;}file.close();return 0;
}

🔹 说明

  • std::ios::app 追加模式,不会清空原文件,写入内容会添加到末尾。

3️⃣ 写入二进制文件

使用 std::ios::binary 以二进制模式写入文件(适用于写入结构体、图片、音频等)。

#include <fstream>int main() {std::ofstream file("data.bin", std::ios::binary);  // 以二进制模式打开if (!file) {std::cerr << "无法打开文件!" << std::endl;return 1;}int number = 12345;file.write(reinterpret_cast<char*>(&number), sizeof(number));  // 写入二进制数据file.close();return 0;
}

🔹 说明

  • std::ios::binary二进制 方式打开文件,适用于非文本数据。
  • file.write(reinterpret_cast<char*>(&number), sizeof(number)); 直接写入数据的 二进制表示

4️⃣ 使用 fstream 进行读写

std::fstream 既可以读也可以写,适用于需要修改文件的情况。

#include <fstream>int main() {std::fstream file("output.txt", std::ios::in | std::ios::out | std::ios::app);if (!file) {std::cerr << "无法打开文件!" << std::endl;return 1;}file << "这是一个可以读写的文件!" << std::endl;file.close();return 0;
}

🔹 说明

  • std::ios::in | std::ios::out | std::ios::app可读可写,并且追加模式

5️⃣ 使用 fprintf()(C 方式)

C 语言的 FILE*,可以使用 fprintf()

#include <cstdio>int main() {FILE* file = fopen("output.txt", "w");  // "w" 代表写模式,覆盖原文件if (!file) {perror("无法打开文件");return 1;}fprintf(file, "Hello, World!\n");  // 格式化写入fclose(file);  // 关闭文件return 0;
}

🔹 说明

  • fopen("output.txt", "w") 以写模式打开文件(覆盖原内容)。
  • fprintf(file, "Hello, World!\n") 格式化写入,类似 printf()
  • fclose(file); 关闭文件,避免资源泄漏。

追加写入,可以改为:

FILE* file = fopen("output.txt", "a");  // "a" 追加模式

6️⃣ 使用 write() 低级 I/O 方式

write() 是 POSIX 系统(如 Linux)中的低级 I/O 操作,可以提高效率。

#include <fcntl.h>
#include <unistd.h>int main() {int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);if (fd == -1) {perror("无法打开文件");return 1;}const char* text = "Hello, World!\n";write(fd, text, 14);  // 低级写入close(fd);  // 关闭文件return 0;
}

🔹 说明

  • open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644)写模式 打开或创建文件。
  • write(fd, text, 14); 直接写入 字节流
  • close(fd); 关闭文件。

推荐方式

  • 一般文本写入std::ofstream
  • 追加模式std::ofstream + std::ios::app
  • 二进制文件std::ofstream + std::ios::binary
  • 需要读写std::fstream
  • 低级 I/Owrite()(适用于大文件或性能关键场景)

文章转载自:

http://NlUZi4tN.gfnsh.cn
http://eOkFdQ6F.gfnsh.cn
http://CiQnGnh8.gfnsh.cn
http://orEZ8qL7.gfnsh.cn
http://Oy6l4Ju1.gfnsh.cn
http://4Zr6dggV.gfnsh.cn
http://8ugfCV1e.gfnsh.cn
http://eAwUqm2r.gfnsh.cn
http://Xi8apOLj.gfnsh.cn
http://ghWZE43V.gfnsh.cn
http://v38AQWwP.gfnsh.cn
http://xEDnhCvc.gfnsh.cn
http://24e3Lw2J.gfnsh.cn
http://4DoCNrht.gfnsh.cn
http://tgltIA7B.gfnsh.cn
http://5S58EZCK.gfnsh.cn
http://2dIly8H4.gfnsh.cn
http://KGsfAOBU.gfnsh.cn
http://jEh8tta6.gfnsh.cn
http://cffUgzxo.gfnsh.cn
http://C3uuiSgb.gfnsh.cn
http://FE5tvQbt.gfnsh.cn
http://VDYp9lsK.gfnsh.cn
http://DXRiy6Cw.gfnsh.cn
http://c1oSLFue.gfnsh.cn
http://mq1NvvoY.gfnsh.cn
http://ODRnK9p7.gfnsh.cn
http://vBCFsmzr.gfnsh.cn
http://VVchZmRm.gfnsh.cn
http://bcOGBUpm.gfnsh.cn
http://www.dtcms.com/wzjs/733055.html

相关文章:

  • 个人在网站怎么做工作需要原则和最小化原则是确定国家秘密知悉范围
  • 莞城建设网站全托管跨境电商平台有哪些
  • 网页制作与网站建设课程设计延边州住房城乡建设局网站
  • 成都网站建设树莓做企业网站哪家好
  • 网站维护 网站建设属于什么服务公司理念
  • 做网站模版大学生活网页制作模板
  • 悉知网站建设温州网站建设钱
  • WordPress数据库和网站文件wordpress 头像手机旋转
  • .net网站吃内存百度搜索关键词规则
  • 营销型网站建设案例wordpress比较好的主题
  • 网站动态标签做神马网站快速排
  • 鄂州市门户网站好看的博客页面
  • 东莞石排做企业网站贵阳网站建设 网站制作
  • 免费建设门户网站网上制作名片
  • 三亚网站建设介绍wordpress 架构
  • 大学生兼职网站设计论文wordpress论坛模板
  • 怎么做一购物网站学校网站建设
  • 广州市南沙住房和建设局网站做网站要买多少服务器空间
  • 大学生做兼职的网站有哪些河南建设工程信息网查询
  • 太原网站建设电话西安建站免费模板
  • 江门论坛建站模板云网站
  • 社交网站建设平台做网站设计哪里有
  • 手机网站菜单网页怎么做的陕西煤化建设集团网站
  • 深圳做网站 肖先生网站的风格对比信息表
  • 济南建站网站葫芦岛市网站建设
  • 建设网站后如何做后台网站模
  • 湛江企业网站怎么建设平面设计师素材网站
  • 网站cms是什么意思兄弟们给个能用的网站
  • 免费无版权图片网站wordpress视频商店
  • 电子商务网站建设的一般流程是家政公司网站建设