读取两个包含两个 5X5 数组的 .txt 文件时,以下代码工作正常。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string myFile, mysecondFile, mystring;
string DIR;
string extension;
int total = 0;
int number_of_lines = 0;
string line;
extension = ".txt";
DIR = "H:\\Year2\\EE273\\EE273\\Week6\\";
cout << "Enter the name of the file: \t";
cin >> myFile;
cout << "Enter the name of the second file: \t";
cin >> mysecondFile;
myFile = DIR + myFile + extension;
mysecondFile = DIR + mysecondFile + extension;
ifstream inFile;
ifstream inFile2;
int i=5;
int j=5;
int i2=5;
int j2=5;
int i3=5;
int j3=5;
int k;
int l;
int Array[5][5];
int Array2[5][5];
int Array3[5][5];
string attempt1,attempt2;
int row = 0;
int col = 0;
int row2 = 0;
int col2 = 0;//i = row
//y = column
inFile.open(myFile.c_str());
if (!inFile) {
cout <<"Error opening file"<<myFile<<endl;
return -1;
}
while (!inFile.eof())
{
getline(inFile, attempt1);
stringstream iss( attempt1 );
string result;
col = 0;
while (getline( iss, result, ','))
{
//cout << result << endl;
Array[row][col] = atoi(result.c_str());
//j = j + 1;
col = col + 1;
}
row = row + 1;
}
inFile.close();
inFile2.open(mysecondFile.c_str());
if (!inFile2) {
cout <<"Error opening file"<<mysecondFile<<endl;
return -1;
}
while (!inFile2.eof())
{
getline(inFile2, attempt2);
stringstream iss( attempt2 );
string result2;
col2 = 0;
while (getline( iss, result2, ','))
{
//cout << result2 << endl;
Array2[row2][col2] = atoi(result2.c_str());
col2 = col2 + 1;
}
row2 = row2 + 1;
}
inFile2.close();
/*for (int i=0;i<5;i++){
for (int j=0; j<5; j++){
cout<<Array[i][j]<<endl;}}
for (int i2=0;i2<5;i2++){
for (int j2=0; j2<5; j2++){
cout<<Array2[i2][j2]<<endl;
}}
我在这里执行两个矩阵之间的乘法并将结果值写入第三个矩阵。
int Total=0;
i=0;
j2=0;
j=0;
j3=0;
for (i3=0; i3<5; i3++) {
while(j3<5){
while (j<5){
for (i2=0;i2<5;i2++){
Total += Array[i][j]*Array2[i2][j2];
j++;
Array3[i3][j3]=Total;
}}
j=0;
j2++;
j3++;
Total=0;
}
i++;
j=0;
j2=0;
j3=0;
Total=0;
}
我的问题是:修改代码的最简单方法是什么,以便它可以读取两个包含任意大小数组的.txt 文件,然后成功执行乘法?
EDIT 我必须只使用数组来执行此操作,不幸的是我不能使用 vector 。
我认为涉及新 运算符是否正确?
最佳答案
“最简单”的方法是做一些天真的事情,比如完全读取文件一次以获得行数/列数,然后再次读取文件以实际将值存储在矩阵中:
unsigned int rows = 0;
unsigned int cols = 0;
std::string line;
while (std::getline(inFile, line)) {
rows++;
std::stringstream ss(line);
std::string col;
while (std::getline(ss, col, ',')) {
cols++;
}
}
// Now allocate the rows*cols matrix
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
// and read your values into the matrix ...
// matrix[m][n] = xxx
读取一个文件两次是非常低效的;还有其他方法可以预先获取大小。例如,您可以在输入文件中约定在数据之前包含矩阵宽度/高度:
[infile.txt]
3,3
1,2,3
4,5,6
7,8,9
现在您可以阅读文件的第一行,您会知道该文件的其余部分包含一个 3x3 矩阵。使用 new
分配您的矩阵(类似于上面的示例),然后继续将文件的其余部分读入其中。
记住使用delete[]
清理动态分配的矩阵。每次调用 new
时,应该调用 1 次 delete
。
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
关于c++ - 读取任意大小的任意数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27485504/