-5

I really have no idea, how to explain this question. But here it is... Suppose I have an external text file, and it's content goes like this:

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

then i've used this code:

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

this code splits the text lines and outputs the following

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

What I actually want is to create a new structure in each loop and create it's 5 variables to hold each of the porperties for every individual game. For Example

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

Now from the output:

1
Crysis
2012
Crytek
Ubisoft

As the loop runs I want to create a new "Game" Structure and assign these values to it's variables and then push the structure in to a vector.

Here is a summary of what I am trying to create and how far I have come:

The program is a database of games. The user is able to add,delete,search and edit any item from the database. As the program runs and the user adds a Game it successfully gets written in the external .txt file and also get pushed to the end of the vector. Now that is all fine. But when I close the program and run it again, there is data in the text file but the vector is empty. So i want the vector to get populated again with the data in the .txt file, so that the user can continue working with the database.

I dont know if I explained the problem well enough. Or I might have over explained it. I am actually a noob in C++.

Thanks in advance..


here is the full code for the program i am working on...


#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;
}
Danish Ajaib
  • 109
  • 1
  • 11
  • 1
    exact duplicate of stackoverflow.com/questions/23557071/how-to-read-data-from-a-line-in-a-txt-file-into-a-structure-in-c) – Rakib May 10 '14 at 08:48

2 Answers2

0

You can create a struct as shown here.

In the example, they have an array of structs. In order to access an element of the struct use the . operator.

In C++11, you can use a tuple, instead of a struct.

Tuple vs Struct - Stackoverflow.

The idea is that the first can provide more generic operations, while the second has far better readability and you do not have to remember the order of your data members, IMHO.

[EDIT]

AS R. Hasan states, the question is a duplicate of this question.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

I am not sure that's what you are looking for, but consider this (and don't forget to include vector)

First, we define custom structure

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

Now we overload operator>> (it's similar to using cin >> someVariable). Fstream already has methods to write in variables of standard types.

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) ... 
*/

Standard operator>> returns reference to fstream, so we are able to write everything in just 1 line of code. If writing does fail (e.g. end of file reached) we return false.

std::vector<Game> vec; 

STL vector - standard container, that contains your Game structs

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
Kirill
  • 65
  • 7