C++中文本的读入
读入文本文件
标准库包含逐行读取文本文件的功能。然后,你可以一次一行地解析文本文件的每一行。
比如说,你有文件,其中使用数字和逗号表示一个 3x4 的矩阵:
, , , 10.5
, 15.2, ,
, , , 7.5
你想读入这个文件,并创建一个二维矢量来表示矩阵。下面是参考代码。你可以在下面运行代码,看看它是否可以运行。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector> using namespace std; int main() { // initialize string variables for reading in text file lines
string line;
stringstream ss; // initialize variables to hold the matrix
vector < vector <float> > matrix;
vector<float> row; // counter for characters in a text file line
float i; // read in the file
ifstream matrixfile ("matrix.txt"); // read in the matrix file line by line
// parse the file if (matrixfile.is_open()) {
while (getline (matrixfile, line)) { // parse the text line with a stringstream
// clear the string stream to hold the next line
ss.clear();
ss.str("");
ss.str(line);
row.clear(); // parse each line and push to the end of the row vector
// the ss variable holds a line of text
// ss >> i puts the next character into the i variable.
// the >> syntax is like cin >> some_value or cout << some_value
// ss >> i is false when the end of the line is reached while(ss >> i) {
row.push_back(i); if (ss.peek() == ',' || ss.peek() == ' ') {
ss.ignore();
}
} // push the row to the end of the matrix
matrix.push_back(row);
} matrixfile.close(); // print out the matrix
for (int row = ; row < matrix.size(); row++) {
for (int column = ; column < matrix[row].size(); column++) {
cout << matrix[row][column] << " " ;
}
cout << endl;
}
} else cout << "Unable to open file"; return ;
}
下面是从文本文件读取数据的示范。代码读取包含矩阵数据的一个图块文件。然后代码将矩阵输出到终端显示器。通读代码,想想它的功能是什么。然后按“测试运行”按钮运行代码。
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector> using namespace std; int main() { // initialize string variables for reading in text file lines
string line;
stringstream ss; // initialize variables to hold the matrix
vector < vector <float> > matrix;
vector<float> row; // counter for characters in a text file line
float i; // read in the file
ifstream matrixfile ("matrix.txt"); // read in the matrix file line by line
// parse the file if (matrixfile.is_open()) {
while (getline (matrixfile, line)) { // parse the text line with a stringstream
// clear the string stream to hold the next line
ss.clear();
ss.str("");
ss.str(line);
row.clear(); // parse each line and push to the end of the row vector
while(ss >> i) {
row.push_back(i); if (ss.peek() == ',' || ss.peek() == ' ') {
ss.ignore();
}
} // push the row to the end of the matrix
matrix.push_back(row);
} matrixfile.close(); // print out the matrix
for (int row = ; row < matrix.size(); row++) {
for (int column = ; column < matrix[row].size(); column++) {
cout << matrix[row][column] << " " ;
}
cout << endl;
}
} else cout << "Unable to open file"; return ;
}
matrix.txt
, , , 10.5
, 15.2, ,
, , , 7.5
fstream 提供读入和输出文件的函数和类。
这行代码读取文件 “matrix.txt”,然后创建一个名为 “matrixfile” 的对象,你可以使用该对象读入文本文件:
ifstream matrixfile ("matrix.txt");
下面的 if 语句检查文件是否正确打开:
if (matrixfile.is_open()) {
然后 while 循环一次读取一行文件。每行都放在一个名为 “line” 的变量里:
if (matrixfile.is_open()) {
while (getline (matrixfile, line)) {
如果你查看文本文件,可以看到本例中每一行都是由浮点数、逗号和空格组成的字符串。例如,"1, 6, 2, 10.5"。
标准库中的 sstream 文件提供了操作和解析字符串的功能。在代码中你会看到,首先声明了一个 sstream 对象,然后使用 ss 对象遍历并解析文本文件的每一行:
stringstream ss; .... ss.clear();
ss.str("");
ss.str(line); while(ss >> i) {
row.push_back(i); if (ss.peek() == ',' || ss.peek() == ' ') {
ss.ignore();
}
}
换句话说,代码找到了一个浮点数,并将该数添加到名为 rows 的向量中。ss.peek()
这一行查看下一个字符,检查它是逗号还是空格,并忽略逗号或空格。
同样需要注意的是,当你完成读入文件时,一定要保持关闭文件的好习惯。
matrixfile.close();
如果你编写的程序可以同时打开许多文件,并且永远不会关闭文件,那么程序可能会崩溃。
C++中文本的读入的更多相关文章
- DOM中文本节点索引方法
问题 对于 jquery 接口text()只能取到有标签的 dom对象中 文本内容. 如果索引对象本身就是文本节点,则不好索引到, 没有相关的索引选择器. 例如: 对于<input>aaa ...
- Asp.Net中文本换行
Asp.Net中文本换行 VB.NET Function HtmlCode(ByVal fString) If fString <> "" Then ...
- css中文本框与按钮对不齐解决方案
我们先对对input标记设定样式,代码如下: html 代码 <form> <input type=”text” name=”text1” id=”text1” /> < ...
- C#中文本模板(.tt)
关于C#中文本模板(.tt)的简单应用 这两天做项目突遇 .tt文件,之前没有接触过,so查询学习做笔记,帮助记忆和后来者. 在项目添加中点击选择文本模板 下面贴出代码,做了简单的注释 1 2 3 4 ...
- T4((Text Template Transformation Toolkit))模版引擎之基础入门 C#中文本模板(.tt)的应用
1 关于C#中文本模板(.tt)的简单应用https://blog.csdn.net/zunguitiancheng/article/details/78011145 任何一个傻瓜都能写出计算机能理解 ...
- 从NLP任务中文本向量的降维问题,引出LSH(Locality Sensitive Hash 局部敏感哈希)算法及其思想的讨论
1. 引言 - 近似近邻搜索被提出所在的时代背景和挑战 0x1:从NN(Neighbor Search)说起 ANN的前身技术是NN(Neighbor Search),简单地说,最近邻检索就是根据数据 ...
- 【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
原文:[msdn wpf forum翻译]TextBox中文本 中对齐 的方法 原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/threa ...
- 如何设置HTML页面中文本的字体
字体属性介绍 CSS中的字体属性是干什么的呢?字体字体肯定和字体有关咯,就是设置HTML页面中文本的字体, CSS中常用的字体属性有几种呢,笔者给大家梳理了下,比较常用的一共有5种,今天我们就看看这5 ...
- CSS中文本继承情况
无继承性的属性 http://www.cnblogs.com/thislbq/p/5882105.html vertical-align: 垂直文本对齐 CSS中文本可以继承父级样式 体 ...
随机推荐
- C#多线程之间事件通知
我有两个线程,线程1接受网络数据,存到队列;线程2取队列,进行各种复杂的处理然后绘制到界面上;想让线程1有数据了通知线程2,线程2再取队列,因为不通知的话,线程2一直在while循环检索队列时候有东西 ...
- 在多版本python的pip的安装与对应包的安装
最近花了好长时间在搞这个,由于Deepin下python有两个版本,并且都没有安装pip,之前的博文默认安装pip给python2.7,结果各种问题,在此将之前走过的弯路整合起来: 首先,安装pip ...
- Wireless Network POJ - 2236 (并查集)
#include<iostream> #include<vector> #include<string> #include<cmath> #includ ...
- 【arc077f】AtCoder Regular Contest 077 F - SS
题意 给你一个形如"SS"的串S,以及一个函数\(f(x)\),\(x\)是一个形如"SS"的字符串,\(f(x)\)也是一个形如"SS"的 ...
- 洛谷 P3742 umi的函数【构造】
题目背景(https://www.luogu.org/problemnew/show/P3742) umi 找到了一个神秘的函数 f. 题目描述 这个函数接受两个字符串 s1,s2.这些字符串只能由小 ...
- html转换成pdf
指定html转换成pdf 安装插件: npm install --save html2canvas npm install jspdf --save 引入 plugins/ htmlToPdf.js ...
- js顺序加载与并行加载
前端优化过程中常提到js的加载方式,下面说下几种常用的加载方式: 1:head标签内插入<script>标签 <script type="text/javaScript&q ...
- 使用Cmder 安装 Composer 出现 "attempt to call a nil value"
原因: 不是这个原因,也不是那个原因,而是采用了中文路径, 把comder 整个搬到其他目录就行了
- 微服务开源生态报告 No.8
「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...
- 洛谷P1929 迷之阶梯
P1929 迷之阶梯 题目描述 在经过地球防卫小队的数学家连续多日的工作后,外星人发的密码终于得以破解.它 告诉我们在地球某一处的古老遗迹中,存在有对抗这次灾难的秘密武器.防卫小队立即赶 到这处遗迹. ...