Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法连续读取两个不同的文件_C++ - Fatal编程技术网

C++ 无法连续读取两个不同的文件

C++ 无法连续读取两个不同的文件,c++,C++,我正在写一个程序,它可以接受3个命令,command1,command2和bye。前两个命令用于读取文件并处理这些文件中的数据 现在的问题是。。它无法连续处理两个不同的文件。例如 command1 testing.txt ... THIS WORKS ... command1 testingagain.txt wrong command! try again! 我希望每次输入任何文件名时,命令都能正常工作。我不确定如何更改代码的结构来实现这一点 while (getline(cin, str1

我正在写一个程序,它可以接受3个命令,command1,command2和bye。前两个命令用于读取文件并处理这些文件中的数据

现在的问题是。。它无法连续处理两个不同的文件。例如

command1 testing.txt
... THIS WORKS ...
command1 testingagain.txt
wrong command! try again!
我希望每次输入任何文件名时,命令都能正常工作。我不确定如何更改代码的结构来实现这一点

while (getline(cin, str1)){

        if (str1 == "bye")
        {
            return 0;

        } else {
            s1.str (str1);
            s1 >> command;
            s1 >> filename;
            ifs.open(filename.c_str()); 

            if (ifs.fail()) {                                                       
                cerr << "ERROR: Failed to open file " << filename << endl;   
                ifs.clear();   

            } else { 

                if (str1 == "command1 " + filename) {
                    command1(filename);

                } else if (str1 == "command2 " + filename) {
                    command2(filename);                                                              

                }   else {
                    cout << "Wrong command! try again!" << endl;

                } 
            }
            ifs.close();
        }
    }
    return 0;
while(getline(cin,str1)){
如果(str1==“再见”)
{
返回0;
}否则{
s1.str(str1);
s1>>命令;
s1>>文件名;
ifs.open(filename.c_str());
如果(ifs.fail()){
cerr
s1.str(str1);
无法按预期工作。您应该每次创建新的
istringstream
对象:

istringstream s1(str);
s1 >> command;
s1 >> filename;
或者在
str()之后添加
clear()


std::string重载
operator==()
仅当
#include
时。那么…@sparta93您的代码中有
#include
行吗?否则如何使用
std::string
呢?我看到字符串可用,并且总是假设它们是作为其他一些include文件的副作用包含的,但后来看到了“一些东西”对于字符串不起作用-直到我包括。
s1.str(str);
s1.clear();
s1 >> command;
s1 >> filename;