C ++将文件解析为结构数组

时间:2018-03-10 22:24:13

标签: c++ arrays opencl

我正在尝试用C ++读取文件并将其解析为结构数组。我有点工作。我可以打印我正在解析/已经解析的行的内容,并且它正确地打印到控制台,但是当迭代数组时(一旦填充了从文件中获取的值),它就打印出无意义。

我的文件内容类似于以下行:

CONINGSBY 2015 11 26 2300 8.4

所以,我在下面创建了结构来保存数据

typedef struct WeatherDataStruct {
    const char* locationName;
    unsigned int year; // Year shouldn't be negative... We didn't have the tech back then...
    unsigned char month; // Month doesn't need to be a massive number since, we only have 12 of them...
    unsigned char day; // Same as month. Can only be 0-31
    unsigned int time; // 24 hour format. So, not going to be negative and an int should suffice.
    long airTemp; // Temp on the day. In celsius
};

我首先从文件中获取行数,然后用于创建上述结构的新数组。然后我将这个新创建的数组和行传递给函数。该函数打开一个流来读取文件。然后我使用下面的while循环来解析文件。

    while (std::getline(_file, currentLine)) {
        std::stringstream iss(currentLine);

        std::string name, sYear, sMonth, sDay, sTime, sTemp;
        if (std::getline(iss, name, ' ') &&
            std::getline(iss, sYear, ' ') &&
            std::getline(iss, sMonth, ' ') &&
            std::getline(iss, sDay, ' ') &&
            std::getline(iss, sTime, ' ') &&
            std::getline(iss, sTemp, ' ')) {

            std::cout << "Name: " << name.c_str() << "\t";

            // Convert the strings into usable data
            unsigned int year = std::stol(sYear),
                time = std::stol(sTime);
            unsigned char month = static_cast<unsigned char>(std::stol(sMonth)),
                day = static_cast<unsigned char>(std::stol(sDay));
            long temp = std::stol(sTemp);

            weatherArray[i++] = { name.c_str(), year, month, day, time, temp };
        }
        else {
            std::cerr << "Error reading file \"" << filename << "\": Couldn't parse file. It doesn't follow the format given. Line: " << i << std::endl;
        }
        //std::cout << std::endl;
        percent = ((double)i / (double)totallines) * 100;
        printf("\r%.2f%%", percent);
    }

解析时控制台的输出是我所期望的,我看到打印的“名称”。一切都很好。尝试读取新数组中任何元素的“locationName”属性时会发生此问题。 VS调试器和控制台都无法读取字符串。

    std::cout << "DATA:\n\t[";

    for (int i = 0; i < lines; i++) {
        printf("%s|%d, ", weatherArray[i].locationName, weatherArray[i].airTemp);
        //std::cout << weatherArray[i].locationName << "|" << weatherArray[i].airTemp << ", ";
    }
    std::cout << "\b\b]" << std::endl;

调试窗口中的数组:https://i.imgur.com/o56qp7m.png

在控制台中输出:https://i.imgur.com/GZ9THVc.png

注意:将结构中的数据更改为std::string但是,我在某处看到OpenCL不喜欢字符串(我希望能够将结构数组发送到内核并执行某些并行操作魔法)。

编辑:

感谢@JustinRandall提供的信息。我通过将结构定义更改为以下内容来解决此问题:

typedef struct WeatherDataStruct {
    char locationName[60] = "Default value";
    unsigned int year; // Year shouldn't be negative... We didn't have the tech back then...
    unsigned char month; // Month doesn't need to be a massive number since, we only have 12 of them...
    unsigned char day; // Same as month. Can only be 0-31
    unsigned int time; // 24 hour format. So, not going to be negative and an int should suffice.
    long airTemp; // Temp on the day. In celsius

    WeatherDataStruct() {}

    WeatherDataStruct(std::string name, unsigned int year, unsigned char month, unsigned char day, unsigned int time, long temp) {
        strcpy_s(locationName, name.c_str());
        this->year = year;
        this->month = month;
        this->day = day;
        this->time = time;
        this->airTemp = temp;
    }

};

0 个答案:

没有答案