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

C++ diff:output.txt:没有这样的文件或目录,c++,arrays,matrix,data-structures,C++,Arrays,Matrix,Data Structures,我试图使用终端在我的linux服务器上运行这段代码,但我一直收到这个错误 diff:output1.txt:没有这样的文件或目录 diff:output3.txt:没有这样的文件或目录 我想做的是将两个矩阵相乘,从任何包含矩阵的.txt文件中获取输入,然后将结果输出到另一个.txt文件。还要检查乘法是否可能包含错误,如将2*2乘以3*2,并在另一个.txt文件中给出错误 这就像检查案件 这是我的密码 #include <iostream> #include <fstream

我试图使用终端在我的linux服务器上运行这段代码,但我一直收到这个错误

  • diff:output1.txt:没有这样的文件或目录
  • diff:output3.txt:没有这样的文件或目录
我想做的是将两个矩阵相乘,从任何包含矩阵的.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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//函数检查数字(字符串格式)是否为双精度
布尔值为双精度(常数字符串和s)
{
istringstream(s);
双d;
在>>d>>ws&&in.eof()中返回;
}
//主要功能
int main(int argc,char*argv[]){
//检查是否传递了所有必需的命令行参数
如果(argc<4)
{

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

// using ofstream constructors.
#include <iostream>
#include <fstream>  

std::ofstream outfile ("test.txt");

outfile << "my text here!" << std::endl;

outfile.close();
//使用流构造函数。
#包括
#包括
标准:流出管(“test.txt”);

outfile你的代码与你的
diff
问题有什么关系?当我在终端上运行它时,我得到了这个错误。@molbdniloalready@drescherjmUnrelated,如果
while(getline(infile1,line)!=NULL,你有一个非常宽松的编译器
实际上是编译的。它与我的不一样。与
NULL
的比较完全是错误的,不应该存在。顺便说一句,这两个文件都会发生错误。