我在一個文本文件中的以下數據:C++。從文本文件中讀取。每個第二段丟失
...
A,Q1,1
S,a1,a1
S,a2,a2
S,a3,a3
A,Q1,1
S,b1,b1
S,b2,b2
S,b3,b3
A,Q1,1
S,c1,c1
S,c2,c2
S,c3,c3
A,Q1,1
S,d1,d1
S,d2,d2
S,d3,d3
A,Q2,1
S,x1,x1
S,x2,x2
S,x3,x3
...
我用下面的代碼來提取數據:
std::string MyClass::getData(const std::string& icao)
{
// Final String
std::string finalData = "";
// Path to the Navigraph Navdata
std::string path = getPath();
std::string dataFilePathSmall = navdataFilePath + "ats.txt";
// Create file streams
std::ifstream ifsSmall(dataFilePathSmall.c_str());
// Check if neither of the files exists.
if (!ifsSmall.is_open())
{
// If it does not exist, return Not Available
finalData = "FILE NOT FOUND";
}
// If the file 'ats.txt' exists, pull the data relevant to the passed ICAO code
if (ifsSmall)
{
// We need to look a line starting with the airway prefixed by "A," and suffixed by ','
const std::string search_string = "A," + icao + ',';
// Read and discard lines from the stream till we get to a line starting with the search_string
std::string line;
// While reading the whole documents
while (getline(ifsSmall, line))
{
// If the key has been found...
if (line.find(search_string) == 0)
{
// Add this line to the buffer
finalData += line + '\n';
// ...keep reading line by line till the line is prefixed with "S"
while (getline(ifsSmall, line) && (line.find("S,") == 0))
{
finalData += line + '\n'; // append this line to the result
}
}
}
// If no lines have been found
if (finalData == "")
{
finalData = "CODE NOT FOUND";
}
}
// Close the streams if they are open
if (ifsSmall)
{
ifsSmall.close();
}
return finalData;
}
現在,我的問題是,由於某種原因,數據的每個第二部分都不被讀取。也就是說,我收到以下內容:
A,Q1,1
S,a1,a1
S,a2,a2
S,a3,a3
A,Q1,1
S,c1,c1
S,c2,c2
S,c3,c3
我找不出什麼東西在我的代碼丟失。提前感謝!
爲什麼被拒絕投票?如果你能提供幫助,時間會更有效率。 –