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

时间:2018-08-25 16:25:46

标签: c++ arrays matrix data-structures

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

  • 差异:output1.txt:没有这样的文件或目录
  • 差异: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;
}

1 个答案:

答案 0 :(得分:0)

确保文件位于您想要代码读取的位置,或者只是在代码内创建文件

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

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

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

outfile.close();

如前所述,确保输出文件(output1.txt和output3.txt)可用并且存在。