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中文本可以继承父级样式 体 ...
随机推荐
- 【maven】maven pom文件详解
maven pom文件详解 最近配置maven中的pom文件,maven中有些属性不太清楚,在这里记录一下 <project xmlns="http://maven.apache.or ...
- Markdown图片
- QT_获取运行进程所在目录路径_2
QString getProcessFullPath(const quint64 &processId) { #ifdef Q_OS_WIN // access process path WC ...
- new操作符实现过程
var obj = new Object(); //创建新对象 一. new是干嘛的? new操作符用来生成一个新的对象, 它后面必须跟上一个函数(否则, 会抛出TypeError异常), 这个函数就 ...
- mysql错误日志目录
在windows下,一般是mysql安装目录下的data目录下 ,扩展名是.err的文件.
- LintCode_181 将整数A转换为B
题目 如果要将整数A转换为B,需要改变多少个bit位? 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 思路 要考虑负数的问题 如果 一正一负 将他们去全部变成正数 后 ...
- 从0开始学习 GitHub 系列之「05.Git 进阶」
关于 Git 相信大家看了之前一系列的文章已经初步会使用了, 但是关于Git还有很多知识与技巧是你不知道的,今天就来给大家介绍下一些 Git 进阶的知识. 1. 用户名和邮箱 我们知道我们进行的每一次 ...
- YouTube上最受欢迎的十大机器学习视频(最新)
2017-05-04 机器之心 选自KDnuggets 作者:Thuy T. Pham 机器之心编译 参与:微胖.黄小天 虽然 YouTube 有很多不错的机器学习视频,但是很难搞清楚是否值得一看,何 ...
- 当node升级后导致webpack打包出错,node-saas出问题的解决办法
报错信息如下: ERROR in ./node_modules/_extract-text-webpack-plugin@3.0.2@extract-text-webpack-plugin/dist/ ...
- check constraints
SELECT constraint_name, constraint_type, column_name, STATUSfrom user_constraints natural join user_ ...