从文件中读取为stdin
Reading from file as stdin
说我有一个具有某些命令的main()函数,对于ex
int main()
{
ofstream myfile;
while(!cin.eof()){
string command; string word;
cin >> command;
cin >> word;
if (command.compare("add") == 0) {
//do Something
}
if (command.compare("use") == 0){
myfile.open(word);
myfile >> //loop back into this loop as stdin
myfile.close();
}
}
myfile的内容将为文件中的每一行带有一个"命令"字段。
拆分工作:
#include <string>
#include <iostream>
void process(std::istream & is)
{
for (std::string command, word; is >> command >> word; )
{
if (command == "add") { /* ... */ continue; }
if (command == "include")
{
std::ifstream f(word); // or "f(word.c_str())" pre-C++11
if (!f) { /* error opening file! */ }
process(f);
continue;
}
// ...
}
}
int main()
{
process(std::cin);
}
相关文章:
- 从stdin读取时子进程挂起(fork/dup2竞争条件)
- 异步提升从stdin读取的asio
- 使用管道从 STDIN 读取分叉进程时出现问题
- 使用 stdin 读取文本文件,然后遍历多个数组
- 如何在使用 gdb 调试期间从 stdin 读取
- C++:在数据写入管道时从 stdin 读取
- C++以缓冲速率从STDIN读取输入
- 使用重定向的stdin C++读取文件
- 从 Python 子进程中的 stdin 读取挂起在 C++/Python IPC 中
- Python等效于从stdin读取的C++代码
- 从 C++ 中的 STDIN 读取
- 从STDIN读取int时的奇怪行为
- QTextStream stdin读取行未暂停输入
- 如何从stdin读取字符串,直到遇到空行
- 为Windows编写一个非阻塞stdin读取函数
- 有没有办法只从C++中的stdin读取满足某些条件的字符
- 从Stdin读取文本文件
- 从stdin读取并将值存储到数组C++中
- 使用 select() 从 stdin 读取 - 以非阻塞方式
- 在C++中从 stdin 读取长度大于 4096 字节的字符串