格式化和查找文本 C++

Formatting and finding text C++

我正在设计一个程序代码,当给定一些 select 机场代码时,它会确定该给定区域一周的日出和日落时间。

我想指出,因为它是针对高级 C++ Class,我不想被交给代码。我会被展示如何去做,而不是让别人为我做。

在这种情况下,我有一个文件 cityinfo.txt 包含以下信息:

APN
 45.07   83.57   E
ATL
 33.65   84.42   E
DCA
 38.85   77.03   E
DEN
 39.75  104.87   M
DFW
 32.90   97.03   C
DTW
 42.23   83.33   E
GRR
 42.88   85.52   E
JFK
 40.65   73.78   E
LAF
 40.42   86.93   E
LAN
 42.77   84.60   E
LAX
 33.93  118.40   P
MBS
 43.53   84.08   E
MIA
 25.82   80.28   E
MQT
 46.53   87.55   E
ORD
 41.98   87.90   C
SSM
 46.47   84.37   E
TVC
 44.73   85.58   E
YYZ
 43.67   79.63   E

输出类似于 运行 它在每个时区代码(e、z、c 等)之后没有刹车

所以我有两个问题:

似乎在你们的帮助下为我找到了答案来自 public c++ 查找函数的来源:

    ifstream fileIn;
    string airportCode, lat, longitude, timeZone, line;
    size_t pos;

    cout << "Please Enter an Airport Code: ";
    cin >> airportCode;

    fileIn.open("cityinfo.txt");
    if (fileIn.is_open())
    {
        while (fileIn.good())
        {
            getline(fileIn, line);
            pos = line.find(airportCode);
            if (pos != string::npos)
            {

                fileIn >> lat >> longitude >> timeZone;
                break;
            }

        }

    }

这是一个格式非常干净的文件。

你应该做这样的事情:

#include <fstream>

ifstream my_ifstream("data_file.txt");  // Create fstream from file.

while ( my_ifstream )
{
    std::string airport;
    double rise;
    double set;
    std::string tz;

    my_ifstream >> airport >> rise >> set >> tz;
    // Store this data somewhere...
}

你试过类似的东西了吗?