从文件中读取特定的单词并将它们存储在对象中
Reading specific words from a file and storing them in an object
我在编码和概念化分配给我的这个项目时遇到了问题。我四处寻找这个问题的答案,但几乎没有运气,也许它真的很明显。我应该提示用户一个文件名,该文件假定具有以下格式:
动物:
名称:[值]
噪音:[值]
腿:[值]
(中间没有空格)
它应该能够读取与文件中一样多的 "animal objects" 并将它们存储在具有 3 个参数(名称、噪声、腿)的动物对象 class 中。
我的问题主要是在读入文件时,我想不出一个好的方法来读取文件和存储信息。这是我目前拥有的代码。对我目前拥有的代码的任何帮助以及存储值的想法。对不起,如果我解释得不好,请要求澄清,提前谢谢你。
cout << "Enter the file name: ";
string fileName;
getline(cin, fileName);
cout << endl;
try
{
ifstream animalFile(fileName);
if (!animalFile.good()) // if it's no good, let the user know and let the loop continue to try again
{
cout << "There was a problem with the file " << fileName << endl << endl;
continue;
}
string line;
while (animalFile >> line) // To get you all the lines.
{
getline(animalFile, line); // Saves the line in STRING.
cout << line << endl; // Prints our STRING.
}
}
catch (...)
{
cout << "There was a problem with the file " << fileName << endl << endl;
}
如果您真的被这种文件格式所束缚,请考虑执行以下操作来读取数据并存储它:
#1。定义一个class Animal
来表示一个动物:
struct Animal
{
std::string name;
int legs;
int noise;
}
#2。定义一个istream& operator >> (istream&, Animal&)
来读取一个这种类型的对象并检查输入的正确性。
std::istream& operator >> (std::istream& lhs, Animal& rhs)
{
std::string buf;
lhs >> buf >> buf >> rhs.name >> buf >> rhs.noise >> buf >> rhs.legs;
}
#3。使用 std::copy
和 std::istream_iterator
从文件中读取所有值到 std::vector
:
std::istream_iterator<Animal> eos;
std::istream_iterator<Animal> bos(animalFile);
std::vector<Animal> zoo;
std::copy(bos, eos, std::back_inserter(zoo));
此代码没有检查输入错误,可以很容易地添加到 istream& operator >> (istream&, Animal&)
。
我在编码和概念化分配给我的这个项目时遇到了问题。我四处寻找这个问题的答案,但几乎没有运气,也许它真的很明显。我应该提示用户一个文件名,该文件假定具有以下格式:
动物:
名称:[值]
噪音:[值]
腿:[值]
(中间没有空格)
它应该能够读取与文件中一样多的 "animal objects" 并将它们存储在具有 3 个参数(名称、噪声、腿)的动物对象 class 中。
我的问题主要是在读入文件时,我想不出一个好的方法来读取文件和存储信息。这是我目前拥有的代码。对我目前拥有的代码的任何帮助以及存储值的想法。对不起,如果我解释得不好,请要求澄清,提前谢谢你。
cout << "Enter the file name: ";
string fileName;
getline(cin, fileName);
cout << endl;
try
{
ifstream animalFile(fileName);
if (!animalFile.good()) // if it's no good, let the user know and let the loop continue to try again
{
cout << "There was a problem with the file " << fileName << endl << endl;
continue;
}
string line;
while (animalFile >> line) // To get you all the lines.
{
getline(animalFile, line); // Saves the line in STRING.
cout << line << endl; // Prints our STRING.
}
}
catch (...)
{
cout << "There was a problem with the file " << fileName << endl << endl;
}
如果您真的被这种文件格式所束缚,请考虑执行以下操作来读取数据并存储它:
#1。定义一个class Animal
来表示一个动物:
struct Animal
{
std::string name;
int legs;
int noise;
}
#2。定义一个istream& operator >> (istream&, Animal&)
来读取一个这种类型的对象并检查输入的正确性。
std::istream& operator >> (std::istream& lhs, Animal& rhs)
{
std::string buf;
lhs >> buf >> buf >> rhs.name >> buf >> rhs.noise >> buf >> rhs.legs;
}
#3。使用 std::copy
和 std::istream_iterator
从文件中读取所有值到 std::vector
:
std::istream_iterator<Animal> eos;
std::istream_iterator<Animal> bos(animalFile);
std::vector<Animal> zoo;
std::copy(bos, eos, std::back_inserter(zoo));
此代码没有检查输入错误,可以很容易地添加到 istream& operator >> (istream&, Animal&)
。