如何从.txt文件中的一行中读取数据到C ++中的结构? [英] How to read data from a line in a .txt file into a structure in C++?

查看:113
本文介绍了如何从.txt文件中的一行中读取数据到C ++中的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个结构:

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

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

Now I have a .txt file in which the content goes like this:

1 2012 FarCry Crytek Ubisoft

现在当我运行程序时,我想要程序到一个新的矢量游戏,并根据外部.txt文件中该行中的数据设置其值。这是可以实现的吗?任何帮助将不胜感激。
这里是我的代码到目前为止:

Now as I run the program I want the program to a new vector Game and set its values according to the data in that line in the external .txt file. Is that achievable? Any help would be appreciated. Here is my code so far:

#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 ​​


  1. 逐行阅读文件(可以使用getline)

  2. 使用 space 字符作为分隔符拆分字符串这里是一个示例

  3. 根据需要转换拆分的令牌(例如将第一个令牌转换为int)。 这里是一个示例

  1. Read file line by line (you can use getline)
  2. Split the string using space character as delimiter here is an example
  3. Convert the split-ed tokens as required (like convert first token to int ). here is an example

解决方案2

如果每行中都有相同数量的令牌可以单独读取

If you have same number of tokens in each line (here 5), then each can be read individually

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

这篇关于如何从.txt文件中的一行中读取数据到C ++中的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆