将外部文本文件中的行拆分为结构?

时间:2014-05-10 08:40:08

标签: c++

我真的不知道,如何解释这个问题。但在这里...... 假设我有一个外部文本文件,其内容如下:

1 Crysis 2012 Crytek Ubisoft
2 FarCry 2009 Crytek Ubisoft
3 Need_For_Speed 1695 EA Ubisoft
4 Assassin'sCreed 2008 Ubisoft Ubisoft
5 COD 2010 Activision Ubisoft

然后我使用了这段代码:

if (fout.is_open())
{
    std::string delim = " ";
    size_t pos = 0;
    std::string token;

    while (getline(fout, line))
    {
        while ((pos = line.find(delim)) != std::string::npos) 
        {
            token = line.substr(0, pos);
            std::cout << token << std::endl;
            line.erase(0, pos + delim.length());
        }
        std::cout << line << std::endl;
    }
}

此代码拆分文本行并输出以下内容

1
Crysis
2012
Crytek
Ubisoft
2
FarCry
2009
Crytek
Ubisoft
3
Need_For_Speed
1695
EA
Ubisoft
4
Assassins'sCreed
2008
Ubisoft
Ubisoft
5
COD
2010
Activision
Ubisoft

我真正想要的是在每个循环中创建一个新结构并创建它的5个变量来保存每个游戏的每个属性。例如

struct Game
{
int id;
string title;
int year;
string developer;
string publisher;
};

现在来自输出:

1
Crysis
2012
Crytek
Ubisoft

当循环运行时,我想创建一个新的“游戏”结构,并将这些值分配给它的变量,然后将结构推入矢量。

以下是我想要创建的内容以及我已经走了多远:

该计划是一个游戏数据库。用户可以添加,删除,搜索和编辑数据库中的任何项目。当程序运行并且用户添加游戏时,它成功地在外部.txt文件中写入并且也被推送到向量的末尾。现在一切都很好。但是当我关闭程序并再次运行它时,文本文件中有数据,但向量为空。所以我希望使用.txt文件中的数据再次填充向量,以便用户可以继续使用数据库。

我不知道我是否足够好地解释了这个问题。或者我可能已经解释了它。我实际上是C ++中的菜鸟。

提前致谢..


这是我正在处理的程序的完整代码...


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


struct  Game

{
    int id;
    int year;
    string title;
    string publisher;
    string developer;
};

void ShowData(vector<Game> myDatabase)
{
        cout << endl;
        for(size_t i = 0; i <= myDatabase.size()-1; i++)
        {
            cout << endl;
            cout << "-------------------------------------------------------"<<endl;
            cout << "                ITEM NO" << " " << i + 1 << " " <<endl;
            cout << "-------------------------------------------------------"<<endl;
            cout << "    TITLE: " << myDatabase[i].title << endl;
            cout << "     YEAR: " << myDatabase[i].year << endl;
            cout << "DEVELOPER: " << myDatabase[i].developer << endl;
            cout << "PUBLISHER: " << myDatabase[i].publisher << endl;

        }

}

int _tmain(int argc, _TCHAR* argv[])
{
    string option;
    int serialNumber = 0;
    int count = 0;
    string line;


    vector<Game> Database;

    fstream fout;
    fout.open("Database.txt");
    if (fout.is_open())
    {
        std::string delim = " ";
        size_t pos = 0;
        std::string token;
        while (getline(fout, line))
        {

            Game newGame;
            std::cout << line << std::endl;
        }

        while (getline(fout, line))
        {

            while ((pos = line.find(delim)) != std::string::npos) 
            {

                token = line.substr(0, pos);
                std::cout << token << std::endl;
                line.erase(0, pos + delim.length());
            }
            std::cout << line << std::endl;
        }
    }
    else
    {
        cout << "There was an error opening the file.";
    }

    cout << endl;
repeat: 
    cout << endl;

    cout << "ADD | SHOW | DELETE | EDIT | SEARCH " << endl;

    cout << "SAY :  ";
    cin >> option;

    if(option == "ADD" || option == "Add" || option == "add")
    {
        serialNumber += 1;
        Game NewGame;
        NewGame.id = serialNumber;
        cout << endl << "Name: ";
        cin >> NewGame.title;
        cout << "Year: ";
        cin >> NewGame.year;
        cout << "Developer: ";
        cin >> NewGame.developer;
        cout << "Publisher: ";
        cin >> NewGame.publisher;
        cout << endl;

        Database.push_back(NewGame);
        cout << endl;
        cout << "Size is: " << Database.size();


        fout <<  serialNumber << " " << Database[Database.size() - 1].title << " " << Database[Database.size() - 1].year << " "<< Database[Database.size() - 1].developer << " " << Database[Database.size() - 1].publisher << endl;


        goto repeat;

    }
    if (option == "SHOW" || option == "Show" || option == "show")
    {
        ShowData(Database);
        goto repeat;
    }

    if(option == "DELETE" || option == "Delete" || option == "delete")
    {
        int choose;
        cout << "Delete Item No: ";
        cin >> choose;
        Database.erase(Database.begin() + (choose - 1));

        cout << endl;
        ShowData(Database);

        goto repeat;

    }
    if(option == "SEARCH" || option == "Search" || option == "search")
    {
        cout << "Search By [ID, TITLE, YEAR, DEVELOPER, PUBLISHER] : ";
        string choose;
        cin >> choose;
        if(choose == "ID" || choose == "Id" || choose == "id")
        {
            int idNumber;
            cout << "ID No: ";
            cin >> idNumber;
            cout << endl;
            cout << "ITEM NO" << "[" << idNumber << "]" <<endl;
            cout << "    TITLE: " << Database[idNumber - 1].title << endl;
            cout << "     YEAR: " << Database[idNumber - 1].year << endl;
            cout << "DEVELOPER: " << Database[idNumber - 1].developer << endl;
            cout << "PUBLISHER: " << Database[idNumber - 1].publisher << endl;

            goto repeat;
        }
        else if (choose == "TITLE" || choose == "Title" || choose == "title")
        {
            string whatTitle;
            cout << "Enter Title: ";
            cin >> whatTitle;

            for(size_t i = 0; i <= Database.size()-1; i++)
            {
                if(Database[i].title == whatTitle)
                {
                    cout << endl;
                    cout << "ITEM NO" << "[" << i + 1 << "]" <<endl;
                    cout << "    TITLE: " << Database[i].title << endl;
                    cout << "     YEAR: " << Database[i].year << endl;
                    cout << "DEVELOPER: " << Database[i].developer << endl;
                    cout << "PUBLISHER: " << Database[i].publisher << endl;
                }
            }
            goto repeat;
        }
        else if (choose == "YEAR" || choose == "Year" || choose == "year")
        {
            int whatYear;
            cout << "Enter Year: ";
            cin >> whatYear;

            for(size_t i = 0; i <= Database.size()-1; i++)
            {
                if(Database[i].year == whatYear)
                {
                    cout << endl;
                    cout << "ITEM NO" << "[" << i + 1 << "]" <<endl;
                    cout << "    TITLE: " << Database[i].title << endl;
                    cout << "     YEAR: " << Database[i].year << endl;
                    cout << "DEVELOPER: " << Database[i].developer << endl;
                    cout << "PUBLISHER: " << Database[i].publisher << endl;
                }
            }

            goto repeat;
        }
        else if (choose == "DEVELOPER" || choose == "Developer" || choose == "developer")
        {
            string whatDeveloper;
            cout << "Enter Developer Name: ";
            cin >> whatDeveloper;

            for(size_t i = 0; i <= Database.size()-1; i++)
            {
                if(Database[i].developer == whatDeveloper)
                {
                    cout << endl;
                    cout << "ITEM NO" << "[" << i + 1 << "]" <<endl;
                    cout << "    TITLE: " << Database[i].title << endl;
                    cout << "     YEAR: " << Database[i].year << endl;
                    cout << "DEVELOPER: " << Database[i].developer << endl;
                    cout << "PUBLISHER: " << Database[i].publisher << endl;
                }
            }
            goto repeat;
        }
        else if (choose == "PUBLISHER" || choose == "Publisher" || choose == "publisher")
        {
            string whatPublisher;
            cout << "Enter Publisher Name: ";
            cin >> whatPublisher;

            for(size_t i = 0; i <= Database.size()-1; i++)
            {
                if(Database[i].publisher == whatPublisher)
                {
                    cout << endl;
                    cout << "ITEM NO" << "[" << i + 1 << "]" <<endl;
                    cout << "    TITLE: " << Database[i].title << endl;
                    cout << "     YEAR: " << Database[i].year << endl;
                    cout << "DEVELOPER: " << Database[i].developer << endl;
                    cout << "PUBLISHER: " << Database[i].publisher << endl;
                }
            }
            goto repeat;
        }
    }
    if (option == "EDIT" || option == "Edit" || option == "edit")
    {
        int whichItem;

        cout << "Enter Item No: ";
        cin >> whichItem;

        cout << endl << "Name: ";
        string name;
        cin >> name;
        Database[whichItem - 1].title = name;
        cout << "Year: ";
        int year;
        cin >> year;
        Database[whichItem - 1].year = year;
        cout << "Developer: ";
        string developer;
        cin >> developer;
        Database[whichItem - 1].developer = developer;
        cout << "Publisher: ";
        string publisher;
        cin >> publisher;
        Database[whichItem - 1].publisher = publisher;
        cout << endl;

        ShowData(Database);

        goto repeat;
    }
    fout.close();   




    system("PAUSE");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您可以创建一个如here所示的结构。

在示例中,它们具有一组结构。要访问struct的元素,请使用.运算符。

在C ++ 11中,您可以使用tuple而不是结构。

Tuple vs Struct - Stackoverflow

我们的想法是,第一个可以提供更多通用操作,而第二个可以提供更好的可读性,而且您不必记住数据成员的顺序,恕我直言。

[编辑]

AS R. Hasan表示,问题与this问题重复。

答案 1 :(得分:0)

我不确定你在寻找什么,但请考虑一下(并且不要忘记包含矢量)

首先,我们定义自定义结构

struct Game //your structure
{
    int id;
    std::string title;
    int year;
    std::string developer;
    std::string publisher;
};

现在我们重载运算符&gt;&gt; (它类似于使用cin&gt;&gt; someVariable)。 Fstream已经有了编写标准类型变量的方法。

bool operator>>(std::fstream& fs, Game& g) 
{
    if (fs >> g.id >> g.title >> g.year >> g.developer >> g.publisher)
        return true;
    return false;
}
/*
fs >> g.id >> g.title equals (fs.operator>>(g.id)).operator>>(g.title) ... 
*/

标准运算符&gt;&gt;返回对fstream的引用,因此我们只需一行代码即可编写所有内容。如果写入失败(例如,文件结束),我们返回false。

std::vector<Game> vec; 

STL vector - 标准容器,包含您的游戏结构

if (fout.is_open())
{
    for ( ; ; )
    {
        Game g; //creating instantiation of your struct Game
        if (fout >> g) //reading from your file
            vec.push_back(g); //1 of STL vector methods, check link to cppreference higher
        else //if reading fails, we break our infinite loop
            break;
    }
}
for (auto x : vec) //going through every element of our vector.
    cout << x.id << " "; //and printing first variable of your structure