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

Reading a file where the user has to provide some parameters

本文关键字:参数 文件 用户 读取      更新时间:2023-10-16

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

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

dim
coins
oponent

我想阅读此文本文件,但在阅读时,要求用户提供特定的响应,例如:

"reads line with the word dim" -> asks user the dimensions -> next line -> "reads line with coins" -> asks user how many coins and so forth until the 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();