【问题标题】:Trouble reading row of doubles from file C++从文件 C++ 中读取双精度行时遇到问题
【发布时间】:2015-03-28 07:33:53
【问题描述】:

出于某种原因,下面的代码将一堆 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:使用更新的代码更新主代码块,如果文件未打开,则处理该代码块。还是不行

编辑3:
我解决了这个问题,我将这一行 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY"~/ClionProjects/MatMult") 添加到我的 CMakeLists.txt 文件中

【问题讨论】:

  • 用字符串流替换输入以进一步将代码简化为最小示例。以后也考虑使用std::vector。最后,您实际上并没有处理无法打开输入文件的情况,我想这是错误所在。如果发生这种情况,只需throw std::runtime_error("failed to open file");。顺便说一句:在返回之前关闭流是不必要的,无论如何你都可以在函数的其他退出路径上省略它。
  • 无关:另外,您的文件名参数类型错误。你拥有的char * const 和你应该拥有的char const *(或const char *)之间存在根本区别。前者是指向可变数据的非可变指针;后者是指向非可变数据的可变指针。你的编译器应该已经警告你了。如果没有,请提高您的警告级别。
  • 您可以像使用return ...; 一样使用throw ...;。只需检查是否打开成功,然后再处理此案。同样检查输入是否真的成功。在单独的文件中尝试此操作,这样您就可以更好地专注于这一问题。
  • 关于你的编辑,我从来没有写过你应该捕获异常,但是当打开输入文件失败时你应该抛出一个异常。不要为现在抓住它而烦恼,以后会发生的。
  • 我尝试了你的代码,它对我来说工作正常 - 正如@UlrichEckhardt 所说,这表明无法打开文件。在打开inFile 之后测试if (!inFile) 看看是不是这样。

标签: c++ file matrix iostream


【解决方案1】:

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

这是我使用的代码:

#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

这不是应该的样子吗?

【讨论】:

  • 是的,问题实际上出在我的 CMake.txt 文件上。当您在不了解您的工具链的情况下进入某事时,就会发生这种情况。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2016-06-10
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多