如何将文本文件中的行读入变量
How can I read lines from a text file into a variable
我知道这里有很多关于从文本文件中读取行并将其存储在 c++ 结构中的问题。但是,这些问题都包含文本文件,这些文件恰好包含他们所需的信息,例如带有 :
的文本文件
Tom 123
Jim 234
但是,如果我的文本文件有类似
的内容怎么办?
// The range of 'horizontal' indices, inclusive
// E.g. if the range is 0-4, then the indices are 0, 1, 2, 3, 4
GridX_IdxRange=0-8
// The range of 'vertical' indices, inclusive
// E.g. if the range is 0-3, then the indices are 0, 1, 2, 3
GridY_IdxRange=0-9
除了 gridX 和 gridY 中的等号之外,我如何才能将数字 0-8 和 0-9 存储在我的结构中?
我尝试了什么:
struct config {
int gridRange[2];
int gridX;
int gridY;
int cityId;
int cloudCover;
int pressure;
std::string cityName;
};
config openFile(std::string filename, config &con) {
std::fstream inputFile(filename.c_str(), std::fstream::in);
if (inputFile.is_open()) {
std::string line;
for (int i = 0; i < 2; i++) {
while (std::getline(inputFile, line)) {
inputFile >> con.gridRange[i];
std::cout << con.gridRange[i]; //to show what is stored in the array
}
}
//std::cout << range << std::endl;
std::cout << std::endl;
return con;
}
else {
std::cout << "Unable to open file" << std::endl;
}
}
忽略结构中的其他变量,因为我还不需要它们。我得到的输出是
// The range of 'horizontal' indices, inclusive
0
我设法通过在 while
循环中添加 if (line[0] != '/' && !line.empty()) {
并创建一个名为 lineArray[5]
的新字符串变量来解决这个问题。该函数现在看起来像这样
config openFile(std::string filename, config &con) {
std::fstream inputFile(filename.c_str(), std::fstream::in);
if (inputFile.is_open()) {
std::string line;
std::string lineArray[5];
int count = 0;
while (std::getline(inputFile, line)) {
if (line[0] != '/' && !line.empty()) {
lineArray[count] = line;
count++;
}
}
std::cout << "Printing out " << lineArray[0] << std::endl;
std::cout << std::endl;
return con;
}
else {
std::cout << "Unable to open file" << std::endl;
}
}
我得到的输出是 Printing out GridX_IdxRange=0-8
。
我知道这里有很多关于从文本文件中读取行并将其存储在 c++ 结构中的问题。但是,这些问题都包含文本文件,这些文件恰好包含他们所需的信息,例如带有 :
的文本文件Tom 123
Jim 234
但是,如果我的文本文件有类似
的内容怎么办?// The range of 'horizontal' indices, inclusive
// E.g. if the range is 0-4, then the indices are 0, 1, 2, 3, 4
GridX_IdxRange=0-8
// The range of 'vertical' indices, inclusive
// E.g. if the range is 0-3, then the indices are 0, 1, 2, 3
GridY_IdxRange=0-9
除了 gridX 和 gridY 中的等号之外,我如何才能将数字 0-8 和 0-9 存储在我的结构中?
我尝试了什么:
struct config {
int gridRange[2];
int gridX;
int gridY;
int cityId;
int cloudCover;
int pressure;
std::string cityName;
};
config openFile(std::string filename, config &con) {
std::fstream inputFile(filename.c_str(), std::fstream::in);
if (inputFile.is_open()) {
std::string line;
for (int i = 0; i < 2; i++) {
while (std::getline(inputFile, line)) {
inputFile >> con.gridRange[i];
std::cout << con.gridRange[i]; //to show what is stored in the array
}
}
//std::cout << range << std::endl;
std::cout << std::endl;
return con;
}
else {
std::cout << "Unable to open file" << std::endl;
}
}
忽略结构中的其他变量,因为我还不需要它们。我得到的输出是
// The range of 'horizontal' indices, inclusive
0
我设法通过在 while
循环中添加 if (line[0] != '/' && !line.empty()) {
并创建一个名为 lineArray[5]
的新字符串变量来解决这个问题。该函数现在看起来像这样
config openFile(std::string filename, config &con) {
std::fstream inputFile(filename.c_str(), std::fstream::in);
if (inputFile.is_open()) {
std::string line;
std::string lineArray[5];
int count = 0;
while (std::getline(inputFile, line)) {
if (line[0] != '/' && !line.empty()) {
lineArray[count] = line;
count++;
}
}
std::cout << "Printing out " << lineArray[0] << std::endl;
std::cout << std::endl;
return con;
}
else {
std::cout << "Unable to open file" << std::endl;
}
}
我得到的输出是 Printing out GridX_IdxRange=0-8
。