c++学习笔记—c++对txt文件的读取与写入
一、文件的输入输出
头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据、ofstream向一个给定文件写入数据、fstream读写给定数据。这些类型与cin和cout的操作一样,我们可以用IO操作符来读写文件,还可以用getline从一个ifstream读取数据。
1、getline()函数
getline的函数原型为:
- istream& getline(istream& is, string& str, char delim);
- istream& getline(istream&& is, string& str, char delim);
- istream& getline(istream& is, string& str);
- istream& getline(istream&& is, string& str);
通常我们使用getline函数读取一整行,该函数接受一个输入流和一个string对象,函数从给定的输入流中读取内容,直到遇到换行符为止,然后将所读的内容存入到个string对象中。
另外,当函数为istream& getline (istream& is, string& str, char delim);形式时,函数遇到delim也会停止。
2、使用文件流对象
当我们想要读入一个文件时,可以定义一个文件流对象,并将对象与文件相关联起来,每一个文件流类都定义了一个名为open的成员函数,完成一系列系统相关的操作。
open函数的原型为:
- void open (const char* filename, ios_base::openmode mode = ios_base::out);
- void open (const string& filename, ios_base::openmode mode = ios_base::out);
文件模式(mode)有一下几种:
- ofstream outfile("E:\\out.txt", ofstream::app);
上述代码打开out.txt文件,如果不存在,系统会创建此txt文件,并且定位到文件末尾。
打开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作。
例:从hello.txt文件中读取数据并写入到out.txt中
- #include "stdafx.h"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- ifstream myfile("E:\\hello.txt");
- ofstream outfile("E:\\out.txt", ofstream::app);
- string temp;
- if (!myfile.is_open())
- {
- cout << "未成功打开文件" << endl;
- }
- while(getline(myfile,temp))
- {
- outfile<<temp;
- }
- myfile.close();
- return 0;
- }
二、string流
string头文件定义了三个类型来支持内存IO,istringstream向string写入数据,ostringstream从string读取数据,stringstream既可从string读取数据也可向string写数据,就像string是一个IO流一样。
- #include "stdafx.h"
- #include <string>
- #include <sstream> //使用istringstream所需要的头文件
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- string str = "I am a boy";
- istringstream is(str);
- string s;
- while (is >> s)
- {
- cout << s << endl;
- }
- return 0;
- }
输出结果为:
I
am
a
boy
例:编写程序,将来自一个文件中的行保存在一个vector<string>中,然后使用istringstream从vector读取数据元素,每次读取一个单词。
- #include "stdafx.h"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- vector<string> vec;
- ifstream myfile("E:\\hello.txt");
- string temp;
- if (!myfile.is_open())
- {
- cout << "未成功打开文件" << endl;
- }
- while(getline(myfile,temp))
- {
- vec.push_back(temp);
- }
- for (auto it = vec.begin(); it != vec.end(); it++)
- {
- cout << *it << endl;
- }
- cout << "-----------------使用istringstream------------------------" << endl;
- for (auto it = vec.begin(); it != vec.end(); it++)
- {
- istringstream record(*it);
- string s;
- while (record >> s)
- cout << s << endl;
- }
- return 0;
- }
运行结果如图所示:
//下述论述转自www.cndev-lab.com ,程序作者:管宁
- #i nclude <iostream>
- #i nclude <sstream>
- using namespace std;
- int main()
- {
- istringstream istr;
- istr.str("1 56.7",);
- //上述两个过程可以简单写成 istringstream istr("1 56.7");
- cout << istr.str()<<endl;
- int a;
- float b;
- istr>>a;
- cout<<a<<endl;
- istr>>b;
- cout<<b<<endl;
- system("pause");
- }
上例中,构造字符串流的时候,空格会成为字符串参数的内部分界,例子中对a,b对象的输入"赋值"操作证明了这一点,字符串的空格成为了整型数据与浮点型数据的分解点,利用分界获取的方法我们事实上完成了字符串到整型对象与浮点型对象的拆分转换过程。
str()成员函数的使用可以让istringstream对象返回一个string字符串(例如本例中的输出操作(cout<<istr.str();)。
ostringstream同样是由一个string对象构造而来,ostringstream类向一个string插入字符。
ostringstream的构造函数原形如下:
- ostringstream::ostringstream(string str);
//下述论述转自www.cndev-lab.com ,程序作者:管宁
- #i nclude <iostream>
- #i nclude <sstream>
- #i nclude <string>
- using namespace std;
- int main()
- {
- ostringstream ostr;
- //ostr.str("abc");//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结 尾开始增加,而是修改原有数据,超出的部分增长
- ostr.put('d');
- ostr.put('e');
- ostr<<"fg";
- string gstr = ostr.str();
- cout<<gstr;
- system("pause");
- }
在上例代码中,我们通过put()或者左移操作符可以不断向ostr插入单个字符或者是字符串,通过str()函数返回增长过后的完整字符串数据,但值 得注意的一点是,当构造的时候对象内已经存在字符串数据的时候,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长。
c++学习笔记—c++对txt文件的读取与写入的更多相关文章
- c# txt文件的读取和写入
我们在工程实践中经常要处理传感器采集的数据,有时候要把这些数据记录下来,有时候也需要把记录下来的数据读取到项目中.接下来我们用C#演示如何对txt文件进行读写操作.我们要用到StreamReader ...
- C# txt文件的读取与写入
C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...
- 《程序实现》从xml、txt文件里读取数据写入excel表格
直接上码 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java ...
- (三)C#关于txt文件的读取和写入
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- c++对txt文件的读取与写入
转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...
- c#中对txt文件的读取与写入,针对二维数组
class Program { ; ; static string[,] str = new string[ROW, COL]; static void Main(string[] args) { R ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- java 学习笔记之 流、文件的操作
ava 学习笔记之 流.文件的操作 对于一些基础的知识,这里不再过多的解释, 简单的文件查询过滤操作 package com.wfu.ch08; import java.io.File; import ...
- Java NIO 学习笔记(四)----文件通道和网络通道
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
随机推荐
- 【Html】Vue动态插入组件
html: <div id="app"> <p>{{ message }}</p> <button @click="add('a ...
- 解救小哈——DFS算法举例
一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二. ...
- 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies
第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...
- Java如何比较两个数组?
在Java中,如何比较两个数组? 示例 以下示例使用equals方法来检查两个数组是否相等. package com.yiibai; import java.util.*; public class ...
- Spring JDBC插入数据
以下示例将展示如何使用Spring jdbc进行插入查询.将向student表中插入几条记录. 语法: String insertQuery = "insert into student ( ...
- e867. 获取和设置外观
To change the look and feel, you need to know the class name of the new look and feel. This example ...
- e611. Setting Focus Traversal Keys for the Entire Application
This example changes the focus traversal keys for the entire application. For an example of how to c ...
- php -- php模拟浏览器访问网址
目前我所了解到的在php后台中,用php模拟浏览器访问网址的方法有两种: 第一种:模拟GET请求:file_get_contents($url) 通过php内置的 file_get_contents ...
- android项目引入三方类库配置文件
android项目中可能会用到诸多外部的三方库,如**.jar或者引用第三个项目,那么它们引用的这些东西都放在哪里呢?我们来看下. 如果引入的是三方的jar包,我们默认的是放在了libs文件夹下,然后 ...
- Linux——ps(列出进程)
ps是Linux系统中用于查看进程状况的命令,用于显示当前系统中进程的快照.ps会显示部分当前活动的进程信息,不同于top指令,top指令会实时的更新所显示的进程动态. Linux的ps指令兼容了多种 ...