C++ file-redirection

前端 未结 4 1952
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 07:28

For faster input, I read that you can do file-redirection and include a file with the cin inputs already set.

In theory it should be used l

4条回答
  •  孤城傲影
    2020-12-31 07:41

    [I am just explaining the command line argument used in Question]

    You can provide file name as command line input to the executible, but then you need to open them in your code.

    Like

    You have supplied two command line arguments namely inputfile & outputfile

    [ App.exe inputfile outputfile ]

    Now in your code

    #include
    #include
    #include
    
    int main(int argc, char * argv[])
    {
       //argv[0] := A.exe
       //argv[1] := inputFile
       //argv[2] := outputFile
    
       std::ifstream vInFile(argv[1],std::ios::in); 
       // notice I have given first command line argument as file name
    
       std::ofstream vOutFile(argv[2],std::ios::out | std::ios::app);
       // notice I have given second command line argument as file name
    
       if (vInFile.is_open())
       {
         std::string line;
    
         getline (vInFile,line); //Fixing it as per the comment made by MSalters
    
         while ( vInFile.good() )
         {
             vOutFile << line << std::endl;
             getline (vInFile,line);          
         }
         vInFile.close();
         vOutFile.close();
      }
    
      else std::cout << "Unable to open file";
    
      return 0;
    }
    

提交回复
热议问题