当前位置: 代码迷 >> 综合 >> C++:读入文本文件
  详细解决方案

C++:读入文本文件

热度:5   发布时间:2023-10-29 03:07:16.0

读取数字和逗号表示一个 3x4 的矩阵:

1, 6, 2, 10.5
11, 15.2, 2, 21
3, 9, 1, 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 matrixvector < vector <float> > matrix;vector<float> row;// counter for characters in a text file linefloat i;// read in the fileifstream matrixfile ("matrix.txt");// read in the matrix file line by line// parse the fileif (matrixfile.is_open()) {while (getline (matrixfile, line)) {// parse the text line with a stringstream// clear the string stream to hold the next liness.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 reachedwhile(ss >> i) {row.push_back(i);if (ss.peek() == ',' || ss.peek() == ' ') {ss.ignore();}}// push the row to the end of the matrixmatrix.push_back(row);}matrixfile.close();// print out the matrixfor (int row = 0; row < matrix.size(); row++) {for (int column = 0; column < matrix[row].size(); column++) {cout << matrix[row][column] << " " ;}cout << endl; }}else cout << "Unable to open file";return 0;
}

代码还有两部分你没看到:fstream 和 sstream。这两个文件都是 C++ 标准库的一部分。

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();