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

时间:2015-02-20 01:57:46

标签: c++ arrays file structure

我完全迷失了,并且已经尝试了几个小时来阅读一个名为" movies.txt"的文件。并将信息存储到数组中,因为它有分号。有帮助吗?感谢。

movies.txt:

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

代码:

#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;
}

我想要的是:

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;

3 个答案:

答案 0 :(得分:1)

我修改了你的代码。

#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;
}

基本上,我添加了一个split函数,它使用';'分割行。我也使用矢量来存储电影而不是硬编码的电影数组。这样好多了。

P.S。没有载体的第二个版本

#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;
}

答案 1 :(得分:0)

使用getline(my_movieFile, movie_name, ';')获取电影的名称,直到;。

如果需要,您需要弄清楚如何从名称中删除尾部空格。您可以搜索示例。

使用getline(movieFile, line)

阅读本行的其余部分

使用std::replace将所有;替换为line

中的空格

line放入std::stringstream

然后使用>>运算符从字符串流中提取剩余的字段。

将其置于循环do { ... } while (movieFile);

此外,不要对任意数量的电影进行硬编码。使用std::vector<Movie>push_back添加新内容。

答案 2 :(得分:0)

我认为您希望使用std::strtok之类的内容将您的行划分为令牌。查看参考here。该页面上给出的示例使用空格作为分隔符,您将使用分号。