Saving a username.

I'm writing a program that has a few usernames for the user to choose from; they then pick their preference and are called that from then on.

It had me thinking... Is there any way to save a username that they enter?

I'm thinking of something with text files.

(If .txt files are the way to go, could you tell me how to locate them from the compiler?)

Thank you,

Ben.
closed account (zb0S216C)
In your situation, a file is preferred. When creating a file, the file is created within the working directory of the program (the directory from which the program is running), unless it's created some place else. Simply load the data from the file during run-time.

Wazzak
There's a problem I'm having.

When doing this:

 
std::ofstream.open(username".txt");

nothing is output.
I know I'm doing it horribly wrong, but is there any other way of getting the username from the user and opening that file?
closed account (zb0S216C)
A basic file I/O operation takes this form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>

int main( )
{
    std::ofstream OStream;
    OStream.open( "UserData.txt" );

    // Check if the file is open.
    if( !OStream.is_open( ) )
        // The file isn't open.
        return 1;

    // Write the users data to the file.
    OStream << "Username: " << "MyUserName" << std::endl;
    OStream << "Password: " << "SecretPassword" << std::endl;

    // Close the file stream.
    OStream.close( );
    return 0;
}

Try taking this code and expand on it.

Wazzak
When I enter this code in, nothing is produced; it goes straight to the end of the program.

Or was that the intention, so that I didn't just copy and paste it?
Last edited on
closed account (zb0S216C)
What do you mean by nothing is produced? All this code does is open a file, write to the file, and then closes the file. It never prompts the client for input.

Wazzak
Sorry, I must have read it wrong.
Topic archived. No new replies allowed.