【问题标题】:Pull data from text file into a struct将文本文件中的数据提取到结构中
【发布时间】:2013-10-12 07:39:23
【问题描述】:

我目前在尝试使用结构从文本文件中提取数据然后将其存储到向量中时遇到问题。但无论我做什么,除非我将 float,int 的值更改为字符串,否则它总是会给我这样的错误:

MissionPlan.cpp:190:错误:从“void*”到“char**”的无效转换
MissionPlan.cpp:190:错误:无法将参数“2”的“float”转换为“size_t*”到“__ssize_t getline(char**, size_t*, FILE*)

这是我的结构:

struct CivIndexDB {
float civInd;
int x;
int y;
}

这是我的示例文本文件:

3.2341:2:3
1.5234:3:4

这是我用来从文本文件中提取数据然后将其存储到向量中的代码:

string line = "";
while (getline(civIndexFile,line)) {
    stringstream linestream(line);

    getline(linestream,civDb.civInd,':');
    getline(linestream,civDb.x,':');
    getline(linestream,civDb.y);
    civIndexesList.push_back(civDb);            
}

将结构中的变量类型更改为字符串不是我需要的,因为稍后在应用程序中,我需要根据其浮点值对向量值进行排序。

感谢您提供的任何帮助。谢谢!

【问题讨论】:

  • 为什么不用二进制文件?我不记得如何编写二进制文件,但我认为在您的示例中它们更合适。
  • 你总是可以读入一个字符串,然后将字符串转换成你在你的结构中需要的数字。但我认为 P0W 的答案是最简单的。
  • @john 如果结构变量是字符串类型,那么存储到向量中的数据必须是字符串类型。即使我将字符串转换为我需要的类型,我仍然需要一个能够存储 float、int、int 的向量(我认为这几乎是不可能的)并稍后对其进行排序。
  • @JoelSeah 我没有说结构变量应该是字符串。我的意思是你可以读入一个 local 字符串变量,然后当你把这个数字放在你的结构中时将这个局部变量转换为一个数字。 string s; getline(linestream,s,':'); civDb.civInd=convertStringToFloat(s);。当然你还是要写 convertStringToFloat 函数。

标签: c++ vector struct text-files


【解决方案1】:

如果不考虑您的确切问题/错误,我建议,如果文件格式已修复,最简单的方法是:

char ch; // For ':'
while (civIndexFile >> civDb.civInd >> ch >> civDb.x >> ch >> civDb.y )
{
    civIndexesList.push_back(civDb); 
}

编辑

为了对浮点值进行排序,您可以重载 < 运算符:

struct CivIndexDB {
  float civInd;
  int x;
  int y;

  bool operator <(const CivIndexDB& db) const
  {
    return db.civInd > civInd;
  }
};

然后使用std::sort

std::sort(civIndexesList.begin(), civIndexesList.end() );

【讨论】:

  • 似乎更改文件格式是我现在最好的选择。但是你能告诉我二进制文件和文本模式文件的区别吗?我阅读了在线资源,但我并不真正了解它们。
  • @JoelSeah 为什么要更改文件格式,float:int:int 将与上述代码完美配合。试试this代码,改文件名
  • 你不是让我把文件从文本改成二进制然后用上面的代码测试一下吗?
  • @JoelSeah 不,我不是刚刚说过,如果它正是您发布的内容,那么最简单的方法就是上面的代码
  • 谢谢。我一直认为从文本文件中获取数据的唯一方法是使用stringstream。如果我没记错的话,结构变量只能作为字符串工作的原因是stringstream 的bcoz。是吗?
【解决方案2】:

这个怎么样?

#include <vector>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>

struct CivIndexDB {
    float civInd;
    int x;
    int y;
};

int main() {
    std::ifstream civIndexFile;
    std::string filename = "data.dat";

    civIndexFile.open(filename.c_str(), std::ios::in);

    std::vector<CivIndexDB> civ;
    CivIndexDB cid;

    if (civIndexFile.is_open()) {
        std::string temp;

        while(std::getline(civIndexFile, temp)) {
            std::istringstream iss(temp);
            int param = 0;
            int x=0, y=0;
            float id = 0;

            while(std::getline(iss, temp, ':')) {
                std::istringstream ist(temp);
                if (param == 0) {
                    (ist >> id) ? cid.civInd = id : cid.civInd = 0;
                }
                else if (param == 1) {
                    (ist >> x) ? cid.x = x : cid.x = 0;
                }
                else if (param == 2) {
                    (ist >> y) ? cid.y = y : cid.y = 0;
                }
                ++param;
            }
            civ.push_back(cid);
        }
    }   
    else {
        std::cerr << "There was a problem opening the file!\n";
        exit(1);
    }

    for (int i = 0; i < civ.size(); ++i) {
        cid = civ[i];
        std::cout << cid.civInd << " " << cid.x << " " << cid.y << std::endl;
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2018-10-18
    • 2020-12-20
    • 2019-09-26
    • 2017-02-18
    相关资源
    最近更新 更多