读取用户必须提供一些参数的文件
Reading a file where the user has to provide some parameters
好的,所以我知道这个问题有点令人困惑(不要马上投反对票,让我解释一下......
我有一个这样的文本文件:
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();
相关文章:
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何使用输入参数作为文件的传递?
- 隐蔽的 gcc 参数到 CMake 文件
- 生成文件文件名参数
- 在实现文件中使用模板参数声明方法
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 如何使输入文本文件成为构造函数参数?c++
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 读取用户必须提供一些参数的文件
- 如何使用 QProcess 设置参数以运行类文件?
- 可变参数模板在目标文件中有重复的符号?
- c++ 文件的参数
- accept(..) 似乎正在修改我给它的文件描述符参数
- C++ 找不到命令行参数(文件)