如何从.text文件中的行读取数据到C ++中的结构?

时间:2014-05-09 05:15:04

标签: c++

假设我有一个结构:

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

现在我有一个.txt文件,其内容如下:

1 2012 FarCry Crytek Ubisoft

现在,当我运行程序时,我希望程序能够使用新的矢量Game,并根据外部.txt文件中该行中的数据设置其值。这可以实现吗?任何帮助,将不胜感激。 到目前为止,这是我的代码:

#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 << "ITEM NO" << "[" << i + 1 << "]" <<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;
    char str[80];
    vector<Game> Database;

    fstream fout;
    fout.open("Database.txt");

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

我要做的是,当我运行程序时,它应该从文本文件中获取所有数据并将它们放在它们的相对结构中,然后将每个结构推送到一个向量中。

1 个答案:

答案 0 :(得分:2)

解决方案1 ​​

  1. 逐行读取文件(可以使用getline)
  2. 使用space字符拆分字符串作为分隔符here是一个示例
  3. 根据需要转换拆分标记(例如将第一个标记转换为int)。 here is an example
  4. 解决方案2

    如果每行中有相同数量的令牌(此处为5),则每个令牌都可以单独读取

    std::ifstream file(fileName);
    struct Game game;
    file >> game.id >> game.year>>game.title>>game.publisher>>game.developer;