无法连续读取两个不同的文件
Unable to read two different files in succession
我正在编写一个程序,它可以接受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;
s1.str (str1);
无法按预期工作。每次都应该创建新的istringstream
对象:
istringstream s1(str);
s1 >> command;
s1 >> filename;
或在str()
:之后添加clear()
s1.str(str);
s1.clear();
s1 >> command;
s1 >> filename;
相关文章:
- 两个文件使用彼此的功能-如何解决
- 结构包含在两个头文件中,这两个文件我都不拥有
- Levenshtein 两个文件的距离花费了太多时间
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 包括两个文件 C++ 之间的冲突
- 如何检测两个文件条目是否引用与Qt相同的物理文件系统?
- 比较两个文件夹的关系
- 连接两个文件时如何避免冗余尾随换行符?
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- 比较两个文件中的两个中间人的最佳方法.C++
- 同时逐行阅读两个文件
- 包含两个C++文件的制作文件:如何选择要编译的文件
- 在 c++ 中对两个文件使用标头
- 同时阅读两个文件
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- 从其他两个文件中创建一个分类整数的文件
- C 程序处理两个文件,而不是一个文件
- 知道分割故障在哪里比较两个文件
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 将相同数据写入两个文件的有效方法