C++ C++;fstream多个输入文件

C++ C++;fstream多个输入文件,c++,file-io,fstream,C++,File Io,Fstream,我正在写一个简单的程序来接收两个文件。终端命令行如下所示 ./fileIO foo.code foo.encode 当它运行时,第二个文件不会被读入。我进去的时候 ./fileIO foo.code foo.code 它起作用了。我似乎不明白为什么第二个没有打开。有什么想法吗?谢谢 #include <fstream> #include <iostream> #include <queue> #include <iomanip> #includ

我正在写一个简单的程序来接收两个文件。终端命令行如下所示

./fileIO foo.code foo.encode
当它运行时,第二个文件不会被读入。我进去的时候

./fileIO foo.code foo.code
它起作用了。我似乎不明白为什么第二个没有打开。有什么想法吗?谢谢

#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;

int main( int argc, char *argv[] )
{
  // convert the C-style command line parameter to a C++-style string,
  // so that we can do concatenation on it
  assert( argc == 3 );
  const string code = argv[1];
  const string encode = argv[2];
  string firstTextFile = code;
  string secondTextFile = encode;

  //manipulate the first infile
  ifstream firstFile( firstTextFile.c_str(), ios::in );
  if( !firstFile ) 
  {
    cerr << "Cannot open text file for input" << endl;
    return 1;
  }

  string lineIn;
  string codeSubstring;
  string hexSubstring;
  while( getline( firstFile, lineIn ) ) 
  {
    hexSubstring = lineIn.substr(0, 2);
    codeSubstring = lineIn.substr(4, lineIn.length() );
    cout << hexSubstring << ", " << codeSubstring << endl;
  }

  //manipulate the second infile
  ifstream secondFile( secondTextFile.c_str(), ios::in );
  if( !secondFile ) 
  {
    cerr << "Cannot open text file for input" << endl;
    return 1;
  }

  char characterIn;
  while( secondFile.get( characterIn ) )
  {
    cout << characterIn << endl;
  }


  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
//将C样式的命令行参数转换为C++样式的字符串,
//这样我们就可以对它进行连接
断言(argc==3);
常量字符串代码=argv[1];
常量字符串编码=argv[2];
字符串firstTextFile=代码;
字符串secondTextFile=encode;
//操纵第一个内嵌
ifstream firstFile(firstTextFile.c_str(),ios::in);
如果(!firstFile)
{

cerr您可能希望尝试的一件事是,在使用完文件后,按照标准过程添加close()调用。如果在上一次运行中未正确关闭文件,则有时会出现重新打开文件的问题

firstFile.close();
secondFile.close();

此外,如果有未释放的延迟文件句柄,您可以尝试重新启动计算机。

什么是“不工作”?我无法再现错误。当我运行程序时,它似乎不想打开第二个文本文件。它运行正常,只是无法打开第二个文本文件。