[英]Using a .txt file to input dataset into a vector (potentially more than one of dataset) in C++
I am working on this project for school that uses JNI and C++ together.我正在为一起使用 JNI 和 C++ 的学校开发这个项目。 The java code was given to me, it gets the user to enter 6 different inputs for a data set regarding a zoo animal.
java 代码给了我,它让用户为有关动物园动物的数据集输入 6 个不同的输入。 At the end it will ask Y or N if you want to add another.
最后它会询问您是否要添加另一个。
If yes the input will be on a second line, once N is selected it will write to a.txt file and the format will be:如果是,则输入将在第二行,一旦选择 N,它将写入 a.txt 文件,格式为:
001235 Alita Mammal Wolf 0 0 001235 阿丽塔哺乳狼 0 0
001234 Bob Mammal Wolf 0 0 001234 鲍勃哺乳动物狼 0 0
(please note there is not an empty line in between the two data) (请注意两个数据之间没有空行)
The code below is what I wrote to open the file and get the inputs into vectors which works successfully for one line of user inputs.下面的代码是我为打开文件并将输入输入到向量中而编写的,该向量可成功用于一行用户输入。 I am running into the issue of how to I make this work logically for a potential unknown amount of vectors a user could enter.
我遇到了一个问题,即如何为用户可以输入的潜在未知数量的向量逻辑地进行这项工作。
void LoadDataFromFile()
{
/*
TODO: Write proper code to load data from input file (generated using JNI) into vector/array.
*/
vector<string> zooVector(6);
int count = 0;
unsigned int i;
ifstream inputFile;
inputFile.open("zoodata.txt");
if (!inputFile) {
cerr << "Unable to open file zoodata.txt";
exit(1); // call system to stop
}
while(count < 6) {
for(i = 0; i < 6; ++i) {
inputFile >> zooVector.at(i);
count += 1;
}
}
The ultimate goal will be to write this to memory for a multi-class inheritance but I am trying to get the inputs to work for the vectors.最终目标是将其写入 memory 以获得多类 inheritance 但我试图让输入适用于向量。
You don't need a vector of strings for the different fields of the animal
;对于
animal
的不同字段,您不需要字符串向量; you need a struct
.你需要一个
struct
。
THEN you'd need a vector of those struct
s to represent a zoo;那么你需要这些
struct
的向量来表示动物园; it will grow dynamically as you read more lines from your file.当您从文件中读取更多行时,它会动态增长。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.