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中文本可以继承父级样式 体 ...
随机推荐
- LAMP环境搭建和配置(2)
配置httpd 默认虚拟主机 编辑hpptd的主配置文件 搜索httpd-vhost,把行首的#号删除 保存主配置文件,然后编辑虚拟主机配置文件 重新编辑配置段(第一段为默认虚拟主机) ServerA ...
- JS文字翻滚效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtm ...
- java实体类的属性名首字母不能大写,不然el表达式无法取值
摘要:Java命名规范中,实体类属性名以小写字母开头,但并没有说不能以大写字母开头,然而事实告诉我,大写真不行 https://www.cnblogs.com/jnhs/p/10025757.html
- 洛谷P1083 [NOIP2012提高组Day2T2]借教室
P1083 借教室 题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借 ...
- Leetcode641.Design Circular Deque设计循环双端队列
设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...
- Phpstrom学习笔记
1.用*标识编辑过的文件 File - Editor – General - Editor Tabs 选中Mark modifyied tabs with asterisk
- Linux下安装配置maven
参考博客: http://www.blogjava.net/caojianhua/archive/2011/04/02/347559.html 注意事项: 1.解压目录 我的maven解压目录为: / ...
- Linux SSH远程链接 短时间内断开
Linux SSH远程链接 短时间内断开 操作系统:RedHat 7.5 问题描述: 在进行SSH链接后,时不时的就断开了 解决方案: 修改 /etc/ssh/sshd_config 文件,找到 Cl ...
- C++ 之手写strcat
char *strcat(char* strDest, const char*strSrc){ assert(strDest != NULL&&strSrc != NULL); cha ...
- IUAP--单点登录
登录组件: 提供统一的登录组件 身份.证明验证身份 支持多种身份标识,用户名.邮箱.手机号 支持多个域,从与得到用户响应的角色,权限进行验证用户时候能进行操作. 支持会话管理和安全管理 支持多种验证策 ...