C ++:从控制台保存信息,然后重新打开时,信息仍然存在

时间:2014-03-29 22:27:00

标签: c++

我有一个程序可以将您喜爱的游戏保存到列表中。我怎么做到这样,如果我关闭控制台并重新打开它,列表仍然存在,并且数组中的所有元素都被保存了?

using namespace std;

int main()
{
    vector<string> games;
    vector<string>::iterator iter;

    while(true)
    {
    system("cls");
    string response;
    cout << "\tFavorite Videos Games \n\n";
    cout << "Type 'add' to add a game.\n";
    cout << "Type 'remove' to remove a game.\n";
    cout << "Type 'list' to list all games.\n\n";
    cout << "Type 'quit' to close and save the program.";
    cout << "What would you like to do: ";
    cin >> response;
    cout << endl;

    if ((response == "add") || (response == "remove") || (response == "list"))
    {
        int number;
        string gameName;
        if(response == "add")
        {
            cout << "What is the name of the game you would like to add?\n";
            cout << "Name: ";
            cin >> gameName;
            games.push_back(gameName);
            cout << gameName << " was added to the system.\n\n";
            Sleep(2000);
            continue;
        }
         if(response == "remove")
        {
            vector<string>::iterator linesIn;
            int spacesIn;
            cout << "What game should be deleted?\n\n";
            cout << "All Games:\n-----------\n";
            for(iter = games.begin(); iter != games.end(); ++iter)
            {
                number ++;
                cout << number << ". " << *iter << endl;
            }
            cout << "Number: ";
            cin >> spacesIn;
            spacesIn = spacesIn -1;
            linesIn = games.begin() + spacesIn;
            cout << *linesIn << " was deleted.";
            games.erase(linesIn);
            Sleep(2000);

        }
         if(response == "list")
        {
            cout << "All Games:\n-----------\n";
            for(iter = games.begin(); iter != games.end(); ++iter)
            {
                number ++;
                cout << number << ". " << *iter << endl;
            }
            cout << "\n\nPress any key to continue...";
            getch();
        }

    }
    else if (response == "quit")
        break;
    else
    {
        cout << "\nInvalid action.\n\n";
        Sleep(2000);
        continue;
    }

    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

最简单的方法是将其保存到文件中。

来自http://www.cplusplus.com/doc/tutorial/files/的示例:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
   ofstream myfile;
   myfile.open ("example.txt");
   myfile << "Writing this to a file.\n";
   myfile.close();
   return 0;
}

也许添加另一个名为save的选项,只需遍历游戏并将其添加到名为“mygames.txt”的文件中。

然后添加一个名为“加载已保存的游戏”的选项,并执行以下操作:

string line;
ifstream myfile ("example.txt");
if (myfile.is_open()){
while ( getline (myfile,line) ){
    games.push_back(line);
}
myfile.close();

显然,这些仅仅是示例,但我觉得很清楚,你可以在这里形成它。

答案 1 :(得分:1)

将信息保存到文件。

#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>

else if ( response == "quit") {
    std::ofstream f( "list.txt");
    if( !f) return -1;
    std::vector<std::string> games;
    std::copy( std::istream_iterator<std::string>(f), // copy file content
               std::istream_iterator<std::string>(), games.begin()); // to vector
}

在节目开始时:

int main()
{
    std::ifstream f( "list.txt");
    if( !f) return -1;
    std::copy( istream_iterator<std::string > (f), // copy file content to vector
            std::istream_iterator<std::string > (), std::back_inserter(games));
    //...

完整示例:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>   
using namespace std;

int main( int argc, char** argv) {
    fstream f;
    f.open( "list.txt", std::ios::in);
    if (!f) return -1;
    vector<string> games;
    copy( istream_iterator<string > (f), // copy file content to vector
            istream_iterator<string > (), back_inserter(games));
    f.close();
    string response;
    while ( 1) {
        cin >> response;
        if ( ( response == "a")) {
            int number; string gameName;
            cout << "What is the name of the game you would like to add?\n";
            cout << "Name: "; cin >> gameName;
            games.push_back(gameName);
            cout << gameName << " was added to the system.\n\n";
        } else if ( response == "q") {
            f.open( "list.txt", std::ios::out);
            if ( !f) return -1;
            copy( games.begin(), games.end(), // copy from vector to a file
                    ostream_iterator<string > ( f, "\n"));
            f.close();
            return 0;
        }
    }
}