文件读取 - C++,使用getline一直读取不到文件中的内容

查看:175
本文介绍了文件读取 - C++,使用getline一直读取不到文件中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

想试着写一个做矩阵运算的代码,然后发现一直都无法使用getline()从文件中读取矩阵。但是我在别的代码中却可以使用getline(),下面是代码。

#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <vector>

using namespace std;
typedef vector<vector<int>> douvec;

//产生一个含有矩阵的文件,这里能够正常的产生矩阵文件
void create_file (fstream &file) {
    int rank;
    //提示输入产生的矩阵的维度
    cout << "input the rank: ";
    cin >> rank;

    //随机生成矩阵的数值
    srand(time(0));
    for (int r = 0; r < rank; r++){
        for (int c = 0; c < rank; c++)
            file << rand()%2 << " ";
        file << '\n';   
    }

}

//讲文件中的矩阵读入一个二维vector中
auto create_matrix (fstream &file) -> vector<vector<int>> {
    char num;
    string line;
    douvec matrix;

    //这里的getline一直都读取不了文件中的任何的数据
    while (getline(file, line)){
        stringstream record(line);
        vector<int> temp;
        if (line == "")
            break;
        while (record >> num){
            int number = (int)num;
            temp.push_back(number);
        }
        matrix.push_back(temp);
    }
    return matrix;
}

int main(){
    fstream file("file.txt", ofstream::app);
    douvec matrix;
    create_file(file);

    //这里输出的matrix.size()一直都是0
    cout << "matrix size is " << matrix.size() << endl;
    matrix = create_matrix(file);
    for (int row = 0; row < matrix.size(); ++row){
        for (int col = 0; col < matrix[row].size(); ++col)
            cout << matrix[row][col] << "  ";
        cout << endl;
    }

    return 0;

}

解决方案

使用getline读不到文件内容是因为不当使用
两个问题

  1. 如果想同时读写一个fstream应该加上对应mode
    fstream file("file.txt", ofstream::in | ofstream::app);

  2. c++ fstream 其实只是把C FILE I/O 重新包装而已。
    c++11 N3337 27.9.1.1

    The restrictions on reading and writing a sequence controlled by an object of class basic_filebuf<charT, traits> are the same as for reading and writing with the Standard C library FILEs.
    In particular:
    If the file is not open for reading the input sequence cannot be read.
    — If the file is not open for writing the output sequence cannot be written.
    — A joint file position is maintained for both the input sequence and the output sequence.

    c11 WG14 N1570 7.21.5.3

    When a file is opened with update mode ('+' as the second or third
    character in the above list of mode argument values), both input and
    output may be performed on the associated stream. However, output
    shall not be directly followed by input without an intervening call to
    the fflush function or to a file positioning function (fseek, fsetpos,
    or rewind), and input shall not be directly followed by output without
    an intervening call to a file positioning function, unless the input
    operation encounters endof-file. Opening (or creating) a text file
    with update mode may instead open (or create) a binary stream in some
    implementations.

    所以只要在matrix = create_matrix(file);前面加上
    file.seekg(0, std::ios_base::beg); 或其他repositioning就可以了。

这篇关于文件读取 - C++,使用getline一直读取不到文件中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆