无法从文件 C++ 中读取双打行
Trouble reading row of doubles from file C++
出于某种原因,下面的代码将一堆 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:
我解决了这个问题,我将这一行 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
这不是应该的吗?
出于某种原因,下面的代码将一堆 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:
我解决了这个问题,我将这一行 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
这不是应该的吗?