I need a function for my text game please!

Hey everyone. I have made 2 different games so for and am currently working on my third one. What I want to do is be able for the user to type is something such as his name, and it be able to remember that by saving it on a file. Then, when you close the program and reopen it, it can read that name from the document and use that in the game, so the person only has to type his name once. Can anyone think of a specific function that could help me? Thanks!
Sure, check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string user_name;

    //attempt to open "name.txt"
    ifstream fin("name.txt");

    //if the file already exists
    //get the name and welcome the user
    if (fin.is_open())
    {
        getline(fin,user_name);
        cout << "Hello, " << user_name << endl;
        cout << "Welcome back!" << endl;
    }

    //if the file doesn't exist
    //create it, ask the user his/her
    //name and store it
    else
    {
        ofstream fout("name.txt");

        cout << "Please, enter your name: ";
        getline(cin,user_name);

        fout << user_name << endl;
        cout << "Ok, I'll remember you..." << endl;
    }

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

Useful links:

http://cplusplus.com/doc/tutorial/files/
http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/string/getline/
YES!! Thanks SO Much!!!!!!!!!!!!!!!!!!!!!!!!!!!
It's a good way but if you want to create a profile for every person which contains other data too, you can save profiles in *.ini files and retrieve them by GetPrivateProfileString() and GetPrivateProfileInt() and write them by WritePrivateProfileString() and WritePrivateProfileInt() functions.
All of them are in windows.h header.
Topic archived. No new replies allowed.