gpt4 book ai didi

c++ - 无法从文件 C++ 中读取 double 行

转载 作者:行者123 更新时间:2023-11-28 06:27:50 25 4
gpt4 key购买 nike

出于某种原因,下面的代码将一堆 0 存储到我的 double 组中,但它不会将 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 文件中

最佳答案

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

这是我使用的代码:

#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

这不是应该的样子吗?

关于c++ - 无法从文件 C++ 中读取 double 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28208554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com