Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数据从文本文件拉入结构_C++_Vector_Struct_Text Files - Fatal编程技术网

C++ 将数据从文本文件拉入结构

C++ 将数据从文本文件拉入结构,c++,vector,struct,text-files,C++,Vector,Struct,Text Files,我目前在尝试使用struct从文本文件中提取数据,然后将其存储到向量中时遇到问题。但无论我做什么,除非我将float,int的值更改为string,否则它总是会给我这样的错误: MissionPlan.cpp:190:错误:从“void*”转换为“char**”无效 MissionPlan.cpp:190:错误:无法将参数“2”的“float”转换为“size\u t*”,将其转换为“\u ssize\u t getline”(char**,size\u t*,FILE*) 这是我的结构: st

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

MissionPlan.cpp:190:错误:从“void*”转换为“char**”无效
MissionPlan.cpp:190:错误:无法将参数“2”的“float”转换为“size\u t*”,将其转换为“\u ssize\u t getline”(char**,size\u 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);            
}
我不需要将struct中的变量类型更改为string,因为在应用程序的后面,我需要根据其浮点值对向量值进行排序


我感谢你给予的任何帮助。谢谢

我建议,在不调查您的确切问题/错误的情况下,如果文件格式已修复,最简单的方法是:

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

对于浮点值排序,您可以重载
,这样如何

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构CivIndexDB{
漂浮雪茄;
int x;
int-y;
};
int main(){
std::ifstream civIndexFile;
std::string filename=“data.dat”;
打开(filename.c_str(),std::ios::in);
std::载体civ;
CivIndexDB-cid;
if(civIndexFile.is_open()){
std::字符串温度;
while(std::getline(civIndexFile,temp)){
标准:istringstream iss(温度);
int参数=0;
int x=0,y=0;
float id=0;
while(std::getline(iss,temp,,:')){
标准::istringstream ist(温度);
如果(参数==0){
(ist>>id)?cid.civInd=id:cid.civInd=0;
}
else if(参数==1){
(ist>>x)?cid.x=x:cid.x=0;
}
else if(参数==2){
(ist>>y)?cid.y=y:cid.y=0;
}
++param;
}
civ.推回(cid);
}
}   
否则{

std::cerr为什么不使用二进制文件?我不记得如何编写二进制文件,但我认为在您的示例中,它们更合适。您始终可以读入字符串,然后将字符串转换为结构中所需的数字。但我认为P0W的答案是最简单的。@john如果结构变量是字符串类型,则数据向量中的OR必须是字符串类型。即使我将字符串转换为我需要的类型,我仍然需要一个能够存储float,int,int的向量(我认为这是不可能的)然后再整理。@JoelSeah我没有说结构变量应该是字符串。我的意思是,当您将数字放入结构中时,您可以读入一个局部字符串变量并将该局部变量转换为一个数字。
string s;getline(linestream,s,,:);civDb.civInd=convertStringToFloat(s)
。当然,您还必须编写convertStringToFloat函数。似乎更改文件格式是我目前的最佳选择。但是您能告诉我二进制文件和文本模式文件之间的区别吗?我阅读了联机源代码,但我并不真正理解它们。@JoelSeah为什么要更改文件格式,
float:int:int
将与上述代码完美配合。请尝试使用代码,更改文件名。您不是要求我将文件从文本更改为二进制,然后使用上述代码进行测试吗?@JoelSeah不,我没有。我只是说,如果这正是您发布的内容,那么最简单的方法就是使用上述代码谢谢。我一直认为,获取数据的唯一方法是rom文本文件要使用
stringstream
。如果我没有错的话,结构变量只作为字符串工作的原因是
stringstream
的bcoz。是吗?
#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;
}