无法从文件C ++中读取双行

时间:2015-01-29 06:48:15

标签: c++ file matrix iostream

由于某种原因,下面的代码将一堆0存储到我的双打数组中,并且它不会将0写入我试图创建的文件中。这是我第一次用c ++编程,所以我还是习惯了一些基本的东西。任何帮助表示赞赏。

#include <iostream>
#include <fstream>
#include <random>

using namespace std;

double* getMatrix(int m, int n, char const*  fileName) {
    ifstream inFile(fileName);
    if(!inFile.is_open()) {
        throw std::runtime_error("failed to open file");
    }
    double* newMatrix = new double[m*n];
    for (int i = 0; i < m * n; ++i) {
        inFile >> newMatrix[i];
    }
    inFile.close();
    return newMatrix;
}

void writeMatrix(int n, int m, double* matrix, char const* fileName) {
    ofstream out(fileName);
    for(int i=0; i < m*n && out; ++i) {
        out << matrix[i] << "\n";
    }
    return;
}

int main(int argc, char *argv[]) {
    double* newMatrix = getMatrix(3, 4, "matrixA.txt");
    for(int i = 0; i < 12; ++i) {
        cout << newMatrix[i] << endl;
    }
    writeMatrix(3, 4, newMatrix, "matrixC.txt");
    delete newMatrix;
}

这是我正在尝试阅读的文件

   0.314723686393179
   0.405791937075619
  -0.373013183706494
   0.413375856139019
   0.132359246225410
  -0.402459595000590
  -0.221501781132952
   0.046881519204984
   0.457506835434298
   0.464888535199277
  -0.342386918322452
   0.470592781760616

编辑:更新了getMatrix函数到下面的内容,现在我在输出中得到错误的值,仍然无法创建文件“matrixC.txt”

double* getMatrix(int m, int n, char* const fileName) {
    ifstream inFile(fileName);
    double* newMatrix = new double[m*n];
    try {
        for (int i = 0; i < m * n; ++i) {
            inFile >> newMatrix[i];
        }
    } catch (ifstream::failure e) {
        cout << "exception opening file";
    }

    inFile.close();
    return newMatrix;
}

这是我得到的输出

-1.28823e-231
-1.28823e-231
6.95324e-310
6.95327e-310
6.95324e-310
6.95327e-310
0
0
0
0
0
0

edit2:使用更新的代码更新主代码块,该代码处理文件是否未打开。仍然没有

EDIT3:
我解决了这个问题,我把这个行集(CMAKE_RUNTIME_OUTPUT_DIRECTORY“〜/ ClionProjects / MatMult”)添加到了我的CMakeLists.txt文件

1 个答案:

答案 0 :(得分:1)

我只是尝试编译并运行您的代码,在快速修复include指令后,它编译得很好,我无法重现您的错误。

这是我使用的代码:

#include <iostream>
#include <fstream>
#include <stdexcept>

using namespace std;

double* getMatrix(int m, int n, char const*  fileName) {
    ifstream inFile(fileName);
    if(!inFile.is_open()) {
        throw std::runtime_error("failed to open file");
    }
    double* newMatrix = new double[m*n];
    for (int i = 0; i < m * n; ++i) {
        inFile >> newMatrix[i];
    }
    inFile.close();
    return newMatrix;
}

void writeMatrix(int n, int m, double* matrix, char const* fileName) {
    ofstream out(fileName);
    for(int i=0; i < m*n && out; ++i) {
        out << matrix[i] << "\n";
    }
    return;
}

int main(int argc, char *argv[]) {
    double* newMatrix = getMatrix(3, 4, "matrixA.txt");
    for(int i = 0; i < 12; ++i) {
        cout << newMatrix[i] << endl;
    }
    writeMatrix(3, 4, newMatrix, "matrixC.txt");
    delete newMatrix;
}

这是我如何建立并运行的:

$ g++ test.cc && ./a.out > test.txt
$ diff test.txt matrixC.txt
$ cat matrixC.txt 
0.314724
0.405792
-0.373013
0.413376
0.132359
-0.40246
-0.221502
0.0468815
0.457507
0.464889
-0.342387
0.470593

这不是它应该是怎么回事?