c++ - 读取用户必须提供一些参数的文件

标签 c++

<分区>

好吧,我知道这个问题有点令人困惑(不要马上投反对票,让我解释一下......)

我有一个这样的文本文件:

dim
coins
oponent

我想阅读这个文本文件,但在阅读时,要求用户给出具体的回应,例如:

“读取带有单词 dim 的行”-> 询问用户尺寸 -> 下一行 -> “读取带有硬币的行”-> 询问用户有多少硬币等等,直到 EOF。

有没有办法做到这一点?如果是,你能告诉我怎么做吗?

谢谢,请不要投反对票,请告诉我哪里出了问题,我会编辑帖子..

编辑:这是我读取文件并询问用户输入的方式

void LeitorFich::lerFicheiro(string nomeFich)
{
    int i, j, t;
    string linha, nome, nome1;
    ifstream fich(nomeFich);

while(getline(fich, linha))
{
    istringstream iss(linha);

    iss >> nome;

    if(nome == "dim")
    {
        cout << nome << " ";
        iss >> i >> j;
    }
}
cin.get();

fich.close();
}

最佳答案

一个简单的例子如下所示:

假设我有一个名为“test.txt”的文件,其中包含与您相同的内容

string sLine;

ifstream inFile("test.txt");

int Dim1, Dim2, coins, oponent;

while(inFile >> sLine ){
    if("dim" == sLine){
        std::cout << "Dim1: ";
        std::cin >> Dim1;
        std::cout << std::endl;

        std::cout << "Dim2: ";
        std::cin >> Dim2;
        std::cout << std::endl;
    }
    else
        if("coins" == sLine){
            std::cout << "coins: ";
            std::cin >> coins;
        }
    else
        if("oponent" == sLine){
            std::cout << "oponent: ";
            std::cin >> oponent;
        }
}

inFile.close();

关于c++ - 读取用户必须提供一些参数的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45871941/

相关文章:

c++ - 如果除法运算符未实现,则 SFINAE 回退

c++ - 是否可以动态绑定(bind) operator>?

c++ - 卡在 boost 邻接列表清除

C++ 类型检查错误

c++ - SDL_FillRect 中的 SDL2 段错误

c++ - C/C++ : Passing a string read to a loop inside a program in C

c++ - 确定场景何时在 Maya 中完成加载

c++ - Eclipse CDT 将 static_assert(cond) 突出显示为语法错误

python - 如何使用 Qt 将可折叠元素(QMenu、QComboBox)渲染到位图缓冲区?

c++ - 大小为 500000 的部分排序数组的快速排序段错误