文件的读写(cpp)
文件的读写(cpp)
c++中要进行文件的读入,首先要包含一个头文件 fstream 。
输出到文件
为打开一个可供输出的文件需要定义一个ofstream 对象并将文件名传入:
std::ofstream out("out.txt");
在不做任何其他操作的情况下,如果该文件不存在就会创建一个相应文件,如果存在就会打开并将原来文件中的信息全部覆盖。如果想要不覆盖原文件而仅仅是在文件的末尾加上要输出的信息,只需要在定义ofstream对象的时候传入第二个参数std::ios_base::app以打开追加模式(append_mode):
std::ofstream out_with_append("out.txt", std::ios_base::app);
此外由于文件可能会打开失败,所以需要用if判断文件是否打开成功。
#include <fstream>
#include <iostream>
int main () {
//输入到文件
std::ofstream out("out.txt");
if (!out) {
std::cerr << "error in out" << std::endl;
} else {
std::cerr << "successfully open out file" << std::endl;
out << "Hello world!" << std::endl;
}
std::ofstream out_with_append("out.txt", std::ios_base::app);
if (!out_with_append) {
std::cerr << "error in out_with_append" << std::endl;
} else {
std::cerr << "successfully open out_with_append file" << std::endl;
out_with_append << "Hello world again!" << std::endl;
}
return 0;
}
这其中std::endl是事先定义好的操作符,由iostream library 提供,他的作用是插入一个换行符并清除输出缓冲区(output buffer)的内容。
从文件中读入
要打开一个可供读入的文件,需要先定义一个ifstream对象并将供于读入的文件传入:
std::ifstream in("out.txt");
同样的文件依然可能会无法正常打开,所以也需要用if判断一下文件是否打开成功。
下面在上面代码的基础上增添ifstream的功能
#include <fstream>
#include <iostream>
#include <string>
int main () {
//输出到文件
std::ofstream out("out.txt");
if (!out) {
std::cerr << "error in out" << std::endl;
} else {
std::cerr << "successfully open out file" << std::endl;
out << "Hello world!" << std::endl;
}
std::ofstream out_with_append("out.txt", std::ios_base::app);
if (!out_with_append) {
std::cerr << "error in out_with_append" << std::endl;
} else {
std::cerr << "successfully open out_with_append file" << std::endl;
out_with_append << "Hello world again!" << std::endl;
}
//从文件中读入
std::ifstream in("out.txt");
if (!in) {
std::cerr << "error in in" << std::endl;
} else {
std::cerr << "successfully open in file" << std::endl;
std::string s;
while (in >> s) {
std::cout << s;
}
}
return 0;
}
这里控制台的全部输出内容为
successfully open out file
successfully open out_with_append file
successfully open in file
Helloworld!Helloworldagain!
Process finished with exit code 0
首先注意到这里在in >> s外面套了一层while,原因是每次读入在没有读到文件末尾的时候 in >> s会返回一个 in,而当读到文件末尾的时候会返回false,因此可以在while的循环表达式中作为结束的标志。
其次会发现输出的内容和输入的内容不一样,原因我引用了下面一段解释
因为istream_iterator的自增的副作用等价于相应流通过>>操作符读一个T类型的数据,而istream通过>>操作符读一个字符类型(这里就是char)的数据会跳过前面的空白字符
这里的空白字符不仅包括了空格,还包括换行符。
与ofstream一样,ifstream同样也有追加模式。为了以追加模式打开文件需要传入第二个参数std::ios::bash::in|std::ios::bash::app :
std::ifstream in("out.txt", std::ios_base::in|std::ios_base::app);
这里需要注意的是以追加模式打开文件会默认在文件的末尾,如果没有重新先定位就读取文件内容,那么就会立即遇到文件结束(EOF)的情况。seekg()可以将in重新定位到文件的起始处。
追加模式作用?
#include <fstream>
#include <iostream>
#include <string>
int main () {
//输出到文件
std::ofstream out("out.txt");
if (!out) {
std::cerr << "error in out" << std::endl;
} else {
std::cerr << "successfully open out file" << std::endl;
out << "Hello world!" << std::endl;
}
std::ofstream out_with_append("out.txt", std::ios_base::app);
if (!out_with_append) {
std::cerr << "error in out_with_append" << std::endl;
} else {
std::cerr << "successfully open out_with_append file" << std::endl;
out_with_append << "Hello world again!" << std::endl;
}
//从文件中读入
std::ifstream in("out.txt", std::ios_base::in|std::ios_base::app);
in.seekg(0);
if (!in) {
std::cerr << "error in in" << std::endl;
} else {
std::cerr << "successfully open in file" << std::endl;
std::string s;
while (in >> s) {
std::cout << s;
}
std::cout << std::endl;
out_with_append << "append text" << std::endl;
in >> s;
std::cout << s << std::endl;
}
return 0;
}
文件的读写(cpp)的更多相关文章
- (转)iOS学习之 plist文件的读写
在做iOS开发时,经常用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ...
- Qt对ini文件的读写
研究了以下Qt下ini文件的读写,不废话,上干货. 写入ini文件 WriteIni.cpp void WriteIni::writeSettings() { QSettings settings(& ...
- Qt对xml文件的读写
最近研究了一下qt下对xml文件的读写,小计一下,成为自己的知识. main函数调用: #include <QApplication> #include "readconfig. ...
- fopen()函数以"a+"方式打开一个不存在的文件后读写出现问题
问题:在完成课后习题的时候,使用fopen()函数以"a+"方式打开一个不存在的文件时,写入.读取出现错误: //添加用户输入单词后,在单词头加入编号,确保编号跟着前面的开始排序 ...
- QT从入门到入土(三)——文件的读写操作
引言 文件的读写是很多应用程序具有的功能,甚至某些应用程序就是围绕着某一种格式文件的处 理而开发的,所以文件读写是应用程序开发的一个基本功能. Qt 提供了两种读写纯文本文件的基本方法: 用 QFi ...
- C#对于文件的读写
C#文件的读写操作 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// 写入txt文件 /// </summary> ...
- java filechannel大文件的读写
java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/ java 读取一个 ...
- c# txt文件的读写
在公司实习,任务不太重,总结一下c#关于txt文件的读写,以便以后有用到的时候可以查看一下.如果有写得不完整的地方还请补充 说明:本人C#水平可能初级都谈不上,高手轻喷,参考:http://www.c ...
- C++中关于文件的读写
在C++的学习过程中,我们时常要用到对文件的操作,下面我们讲一下文件的读写. 首先,读.也就是把已有的文件读到控制台上,那么如何操作呢?首先要将文件操作的输入输出流包含进去. <fstream& ...
随机推荐
- zabbix客户端安装配置
1.下载,解压并安装zabbixtar zxvf zabbix-2.0.12.tar.gzcd zabbix-2.0.12./configure --prefix=/usr/local/zabbix ...
- 【IMP】导出的时候显示ddl建表语句
导出数据后,在导入的时候想要显示出建表语句,可以用show=y这个选项来实现 imp test/test file=test.dmp ignore=y show=y fromuser=test1 to ...
- ctfhub技能树—sql注入—Cookie注入
手注 打开靶机 查看页面信息 查找cookie 测试是否为cookie注入 抓包 尝试注入 成功查询到数据库名 查询表名 查询字段名 查询字段信息 成功拿到flag sqlmap 查询数据库名 pyt ...
- Python批量 png转ico
Python 批量 png 转 ico 一.前言: 首先说一下ico文件的作用:ico是windows的图标文件格式,可以用于浏览器首段图标显示,也可以用于Windows软件.我的话一般用来美化文件夹 ...
- powershell中的cmdlet命令
Add-Computer 向域或工作组中添加计算机. Add-Content 向指定的项中添加内容,如向文件中添加字词. Add-History 向会话历史记录追加条目. Add-Member 向 W ...
- java 利用异或^进行加密
package com.zcj.eg001; import java.nio.charset.Charset; import org.junit.Test; public class Encrypti ...
- uni-app开发经验分享十三:实现手机扫描二维码并跳转全过程
最近使用 uni-app 开发 app ,需要实现一个调起手机摄像头扫描二维码功能,官网API文档给出了这样一个demo: // 允许从相机和相册扫码 uni.scanCode({ success: ...
- 03. struts2中Action配置的各项默认值
Action中的各项默认值 Action各项配置 <action name="helloworld" class="com.liuyong666.action.He ...
- Jmeter如何录制APP客户端脚本
简单五步教大家Jmeter录制APP客户端脚本: Step1 右键单击该测试计划,选择"添加"-"线程组",添加一个线程组. Step2 为了录制客户端的操作, ...
- Linux网卡没有eth0显示ens33原因以及解决办法
原因 首先说明下eth0与ens33的关系: 目前的主流网卡为使用以太网络协定所开发出来的以太网卡 (Ethernet),因此我们 Linux 就称呼这种网络接口为 ethN (N 为数字). 举例来 ...