zoukankan      html  css  js  c++  java
  • C++中文本的读入

    读入文本文件

    标准库包含逐行读取文本文件的功能。然后,你可以一次一行地解析文本文件的每一行。

    比如说,你有文件,其中使用数字和逗号表示一个 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 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 = 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;
    }

    下面是从文本文件读取数据的示范。代码读取包含矩阵数据的一个图块文件。然后代码将矩阵输出到终端显示器。通读代码,想想它的功能是什么。然后按“测试运行”按钮运行代码。

    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 = 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;
    }

    matrix.txt

    1, 6, 2, 10.5
    11, 15.2, 2, 21
    3, 9, 1, 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();

    如果你编写的程序可以同时打开许多文件,并且永远不会关闭文件,那么程序可能会崩溃。

  • 相关阅读:
    spring3.0注解定时任务配置及说明
    程序员,吃什么个对身体好
    log4j 实例 , 浅析
    使用dom4j创建和解析xml
    Java XML解析工具 dom4j介绍及使用实例
    网络原理分析
    Python教程-使用Python解释器
    Jenkins中使用jmeter的脚本
    jmeter: line 129: [: : integer expression expected jmeter: line 200: /usr/bin/java/bin/java: Not a directory解决办法
    阿里云centos安装Jenkins
  • 原文地址:https://www.cnblogs.com/fuhang/p/9056784.html
Copyright © 2011-2022 走看看