简体   繁体   English

从用分号分隔的文件中读取并存储到数组中

[英]Reading from file separated with semicolons and storing into array

I am completely lost and have been trying for hours to read from a file named "movies.txt" and storing the info from it into arrays, because it has semicolons. 我完全迷失了,已经尝试了几个小时从一个名为“ movies.txt”的文件中读取信息,并将其信息存储到数组中,因为它具有分号。 Any help? 有什么帮助吗? Thanks. 谢谢。

movies.txt: movies.txt:

The Avengers     ; 2012     ;  89    ;   623357910.79
Guardians of the Galaxy    ;   2014    ;  96    ; 333130696.46

Code: 码:

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

struct Movie {
    std::string name;
    int year;
    int rating;
    double earnings;
};

int main()
{
    const int MAX_SIZE = 100;
    Movie movieList[MAX_SIZE];
    std::string line;
    int i = 0;

    std::ifstream movieFile;
    movieFile.open("movies.txt");

    while (getline(movieFile, line, ';'))
    {
        movieFile >> movieList[i].name >> movieList[i].year >> movieList[i].rating >> movieList[i].earnings;
        i++;
    }

    movieFile.close();

    std::cout << movieList[0].name << " " << movieList[0].year << " " << movieList[0].rating << " " << movieList[0].earnings << std::endl;
    std::cout << movieList[1].name << " " << movieList[1].year << " " << movieList[1].rating << " " << movieList[1].earnings << std::endl;

    return 0;
}

What I want is to have: 我想要的是:

movieList[0].name = "The Avengers";
movieList[0].year = 2012;
movieList[0].rating = 89;
movieList[0].earnings = 623357910.79;

movieList[1].name = "Guardians of the Galaxy";
movieList[1].year = 2014;
movieList[1].rating = 96;
movieList[1].earnings = 333130696.46;

I amended your code. 我修改了您的代码。

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

struct Movie {
    std::string name;
    int year;
    int rating;
    double earnings;
};


std::vector<std::string>
split(const std::string &s, char delim = ',')
{

    std::vector<std::string> elems;

    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim))
    {
            elems.push_back(item);
    }
    return elems;
}


int main()
{

    std::vector<Movie> movieList;
    std::string line;


    std::ifstream movieFile;
    movieFile.open("movies.txt");

    while (getline(movieFile, line))
    {
        std::vector<std::string> columns = split(line,';');
        Movie movie;

        movie.name     = columns[0];
        movie.year     = std::stoi(columns[1]);
        movie.rating   = std::stoi(columns[2]);
        movie.earnings = std::stof(columns[3]);

        movieList.push_back(movie);
    }

    movieFile.close();

    for (const Movie & m: movieList) 
    {
        std::cout << m.name << " " << m.year << " " << m.rating << " " << m.earnings << std::endl;
    }


    return 0;
}

Basicly, I added a split function that splits the lines using ';'. 基本上,我添加了一个split函数,该函数使用';'分隔行。 Also I use vector to store the movies rather than hard coded array of movies. 另外,我使用矢量来存储电影,而不是硬编码的电影数组。 Much better this way. 这样好多了。

PS Second version without vectors PS第二版,不含向量

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

struct Movie {
    std::string name;
    int year;
    int rating;
    double earnings;
};



void split(const std::string &s, char delim, std::string elems[])
{
    std::stringstream ss(s);
    std::string item;

    int i = 0;

    while (std::getline(ss, item, delim))
    {
            elems[i++] = item;
    }
}


int main()
{

    //std::vector<Movie> movieList;

    const int MAX_SIZE = 100;
    Movie movieList[MAX_SIZE];
    int movieNo = 0;

    std::string line;


    std::ifstream movieFile;
    movieFile.open("/home/marcin/testing/movies.txt");

    std::string columns[4];

    while (getline(movieFile, line))
    {
        split(line,';', columns);

        movieList[movieNo].name     = columns[0];
        movieList[movieNo].year     = std::stoi(columns[1]);
        movieList[movieNo].rating   = std::stoi(columns[2]);
        movieList[movieNo].earnings = std::stof(columns[3]);

        ++movieNo;

    }

    movieFile.close();

    for (int i =0; i < movieNo; ++i) {
        std::cout << movieList[i].name
                  << " "
                  << movieList[i].year
                  << " "
                  << movieList[i].rating
                  << " "
                  << movieList[i].earnings
                  << std::endl;
    }


    return 0;
}

Use getline(my_movieFile, movie_name, ';') to get the name of the movie up to the ;. 使用getline(my_movieFile, movie_name, ';')可以获取电影的名称,直到;;。

You'll need to figure out how to remove the trailing whitespace from the name if necessary.. you can search for examples. 如果需要,您需要弄清楚如何从名称中删除尾随空格。您可以搜索示例。

Read the rest of the line using getline(movieFile, line) 使用getline(movieFile, line)读取其余部分

Use std::replace to replace all ; 使用std::replace替换所有; with a space in line line

Put line into a std::stringstream . line放入std::stringstream

Then extract the remaining fields from the stringstream using the >> operators. 然后使用>>运算符从字符串流中提取其余字段。

Put this in loop do { ... } while (movieFile); 将其放入循环中do { ... } while (movieFile);

Also, don't hardcode an arbitrary number of movies. 另外,请勿硬编码任意数量的电影。 Use a std::vector<Movie> and push_back to add new ones. 使用std::vector<Movie>push_back添加新的。

I think you want to break your line into tokens using something like std::strtok . 我认为您想使用std::strtok类的东西将行划分为令牌。 Check out the reference here . 此处查看参考。 The example given on that page uses a blank as a separator, you would use a semicolon. 该页面上给出的示例使用空格作为分隔符,您将使用分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM