【问题标题】:diff: output.txt: No such file or directorydiff: output.txt: 没有这样的文件或目录
【发布时间】:2019-01-31 19:16:05
【问题描述】:

我尝试使用终端在我的 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;
}

【问题讨论】:

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

标签: c++ arrays matrix data-structures


【解决方案1】:

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

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

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

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

outfile.close();

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

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2016-05-06
    相关资源
    最近更新 更多