diff: output.txt: 没有这样的文件或目录



>im 尝试使用终端在我的 Linux 服务器上运行此代码,但我不断收到此错误。

  • diff: output1.txt: 没有这样的文件或目录
  • diff: output3.txt: 没有这样的文件或目录

IM 试图做的是通过从任何给定包含矩阵的.txt文件中获取输入并将答案输出到另一个.txt文件中来乘以两个矩阵。还要检查乘法可能包含错误,例如将 2*2 乘以 3*2 并在另一个 .txt 文件上给出错误。

这就像检查案件一样。

这是我的代码。

#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <string>
#include <cstdlib>
using namespace std;
//function to check if a number (in string format) is double or not
bool is_double(const string& s)
{
istringstream in(s);
double d;
return in >> d >> ws && in.eof();  
}
//main function
int main(int argc, char* argv[]){
//check if all required command line arguments are passed
if (argc < 4)
{
cerr<<"error"<<endl;
return -1;
}
string inputfile1(argv[1]); //input file containing first matrix
string inputfile2(argv[2]); //input file containing second matrix
string outputfile(argv[3]); //output file containing matrix obtained after multiplication
//extract names of files
inputfile1=inputfile1.substr(inputfile1.find("=")+1);
inputfile2=inputfile2.substr(inputfile2.find("=")+1);
outputfile=outputfile.substr(outputfile.find("=")+1);  
/*cout<<"input file1:"<<inputfile1<<endl;
cout<<"input file2:"<<inputfile2<<endl;
cout<<"output file:"<<outputfile<<endl;*/
//file stream for files
ifstream infile1(inputfile1.c_str(),ifstream::in);
ifstream infile2(inputfile2.c_str(),ifstream::in);
ofstream outfile(outputfile.c_str(), ofstream::out);
if(!infile1 || !infile2 || !outfile)
{
cerr<<"error"<<endl;
return -1;
}
double** inmatrix1; //input matrix 1
double** inmatrix2; //input matrix 2
double** outmatrix; //output matrix
int m=0,n=0; //dimensions of input matrix 1
int p=0,q=0; //dimensions of input matrix 2
//extract dimensions from input file 1
string line="";
while(getline(infile1,line)!=NULL)
{
//count columns
int len = line.size();
int cols = 0;
for(int i=0;i<len;i++)
{
if(!isspace(line[i]))
cols++;
}
n=cols;
m++;
}  
//cout<<"matrix1 dimensions:"<<m<<" "<<n<<endl;
//extract dimensions from input file 2
line="";
while(getline(infile2,line)!=NULL)
{
//count columns
int len = line.size();
int cols = 0;
for(int i=0;i<len;i++)
{
if(!isspace(line[i]))
cols++;
}
q=cols;
p++;
}  
//cout<<"matrix2 dimensions:"<<p<<" "<<q<<endl;
//check if multilplication possible
if(n!=p)
{
cerr<<"error"<<endl;
return -1;
}
//allocate space for matrices
inmatrix1 = new double*[m];
for(int i = 0;i<m;++i)
{
inmatrix1[i]=new double[n];      
}
inmatrix2 = new double*[p];
for(int i = 0;i<p;++i)
{
inmatrix2[i]=new double[q];      
}
outmatrix = new double*[m];
for(int i = 0;i<m;++i)
{
outmatrix[i]=new double[q];      
}
//read data from files into matrices
cout<<"Reading matrix 1..."<<endl;
//matrix 1
infile1.clear();
infile1.seekg(0,ios::beg);
line="";
int j=0,k=0;
while(getline(infile1,line))
{
stringstream ss(line);
string token;
k=0;
while(getline(ss,token,' '))
{
//check if double or not
if(!is_double(token))
{
cerr<<"error"<<endl;
return -1;
}
else
{
inmatrix1[j][k]=atof(token.c_str());
k++;  
}          
}
j++;  
}
cout<<"Matrix 1 read!"<<endl;
//matrix 2
cout<<"Reading matrix 2..."<<endl;
infile2.clear();
infile2.seekg(0,ios::beg);
line="";
j=0,k=0;
while(getline(infile2,line))
{
stringstream ss(line);
string token;
k=0;
while(getline(ss,token,' '))
{
//check if double or not
if(!is_double(token))
{
cerr<<"error"<<endl;
return -1;
}
else
{
inmatrix2[j][k]=atof(token.c_str());
k++;  
}          
}
j++;  
}
cout<<"Matrix 2 read!"<<endl;
//print both matrices
cout<<"Matrix 1:"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<inmatrix1[i][j]<<" ";
}
cout<<endl;
}
cout<<"Matrix 2:"<<endl;
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
cout<<inmatrix2[i][j]<<" ";
}
cout<<endl;
}  
//multiply two matrices
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < q; ++j)
for(int k = 0; k < n; ++k)
{
outmatrix[i][j] += inmatrix1[i][k] * inmatrix2[k][j];
}
}
//print result to file  
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
outfile<<outmatrix[i][j]<<" ";
}          
outfile<<endl;
}  
//close files
infile1.close();
infile2.close();
outfile.close();
return 0;
}

确保文件存在于您希望代码读取它们的位置,或者只是在代码中创建文件

// using ofstream constructors.
#include <iostream>
#include <fstream>  
std::ofstream outfile ("test.txt");
outfile << "my text here!" << std::endl;
outfile.close();

如前所述,请确保您的输出文件(输出 1.txt 和输出 3.txt(可用且存在。

相关内容

最新更新