Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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++读取然后创建对象 我需要一些帮助,从VisualC++中的一个文件中读取。在movie.dat中,我需要阅读以下内容: 2015,Star Wars,DVD,120 1987,Star wars,DVD,110 2010,Inception,DVD,100_C++_Parsing_Visual C++ - Fatal编程技术网

从文件C++读取然后创建对象 我需要一些帮助,从VisualC++中的一个文件中读取。在movie.dat中,我需要阅读以下内容: 2015,Star Wars,DVD,120 1987,Star wars,DVD,110 2010,Inception,DVD,100

从文件C++读取然后创建对象 我需要一些帮助,从VisualC++中的一个文件中读取。在movie.dat中,我需要阅读以下内容: 2015,Star Wars,DVD,120 1987,Star wars,DVD,110 2010,Inception,DVD,100,c++,parsing,visual-c++,C++,Parsing,Visual C++,诸如此类 使用逗号、分隔符和“添加到”字段,如: store.add(year,name,media,length) 在java中,我会这样做: public static void readFromFile(String filename,Store store){ try { Scanner ps = new Scanner(new BufferedInputStream(new FileInputStream(filename)));

诸如此类

使用逗号、分隔符和“添加到”字段,如:

store.add(year,name,media,length)
在java中,我会这样做:

public static void readFromFile(String filename,Store store){
        try {
            Scanner ps = new Scanner(new BufferedInputStream(new FileInputStream(filename)));
            while(ps.hasNextLine()){
                String line = ps.nextLine();
                String field[] = line.split(",");
                store.add(Integer.parseInt(field[0]),field[1],field[2],Integer.parseInt([3]));
            }

            ps.close();
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

首先,我要定义一个结构或类来保存数据:

struct movie { 
    int year;
    std::string name;
    std::string media;
    int length;    
};
然后我将定义一个提取器,从文件中读取其中的一个:

std::istream &operator>>(std::istream &is, movie &m) {
    std::string line;
    std::getline(is, line);
    std::istringstream buffer(line);

    char ignore;
    buffer >> m.year >> ignore;
    std::getline(buffer, m.name, ',');
    std::getline(buffer, m.media, ',');
    buffer >> m.length;
    return is;
}
std::vector<movie> movies {
    std::istream_iterator<movie>(infile),
    std::istream_iterator<movie>() };
然后我从文件中读取一个向量,例如:

std::istream &operator>>(std::istream &is, movie &m) {
    std::string line;
    std::getline(is, line);
    std::istringstream buffer(line);

    char ignore;
    buffer >> m.year >> ignore;
    std::getline(buffer, m.name, ',');
    std::getline(buffer, m.media, ',');
    buffer >> m.length;
    return is;
}
std::vector<movie> movies {
    std::istream_iterator<movie>(infile),
    std::istream_iterator<movie>() };

您应该使用基本的get set函数编写电影类,并将信息存储在电影类对象的向量中

void readMovieDataFromFile(vector<movie> &movies) {

fstream  inputfile;
std::string line;
inputfile.open("C:\\movie.dat", ios::out | ios::in );   

std::string token;

while (std::getline(inputfile, line)) {
    std::istringstream iss(line);
    while (std::getline(iss, token, ','))
    {
        movies[i].setYear(atoi(token.c_str()));

        std::getline(iss, token, ',');
        movies[i].setName(token);

        std::getline(iss, token, ',');
        movies[i].setMedia(token);

        std::getline(iss, token, ',');
        movies[i].setLength(atoi(token.c_str()));
    }
    i++;
}
inputfile.close();
}

谢谢你,这一切对我都有用

使用类似boost::serialization的序列化库。这对于从文件中写入和读取对象非常有效。缺点是它不可读。< / P>你想让我们把java代码转换成C++吗?如果是这样的话,这对StackOverflow来说真的不是一个好问题。不是真的,我只是更熟悉java,所以我尝试用java编写代码,以便更好地理解。。但是我无法找到如何在C++中实现它,因为您需要std::ifstream和std::getline。从这里开始:。要在逗号上拆分,请查看std::getline重载,该重载允许您指定分隔符。如果您使用google stackoverflow读取cvs文件,您将找到许多方法来执行此操作…是的,ofc,我有整个程序和类。。我现在正在做主要的事情:谢谢你男人:不幸的是,这根本不是一个很好的建议。请参阅,以了解有关为什么以及您应该做什么的更多信息。您上面给出的几乎是准类的完美示例,尽管您可能还没有意识到,但它确切地显示了它们所产生的问题。