简体   繁体   中英

How to make a C++ program that works with a .txt file without showing it

My program needs to use a hidden text file to keep track of user's name.

But when the program starts, if it can't find the 'Name.txt' file in the same directory, it generates one that is visible to the user.

The user can view it, edit it, and so on. How can I prevent this from happening, so that only my program can modify the file?

Also, is there a better way to keep knowledge of the name of the user (keep in mind I'm new to programming in general, not only to C++)?

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <Windows.h>

using std::string;
using std::cout;
using std::cin;
using std::ifstream;
using std::ofstream;

int main()
{
    string line;
    ifstream example;
    example.open("Name.txt");
    getline(example, line);
    if (line.compare("") == 0)
    {
        example.close();
        string con;
        cout << "Welcome to this program!\n";
        cout << "Do you want to register? (y/n) ";
        cin >> con;
        con[0] = tolower(con[0]);
        if (con.compare("n") != 0)
        {
            string name;
            ofstream one;
            one.open("Name.txt");
            cout << "What's your name? ";
            cin >> name;
            one << name;
            one.close();
            cout << "See you later " << name << ".";
            Sleep(4000);
        }
    }
    else
    {
        cout << "Welcome back " << line << ".";
        example.close();
        Sleep(4000);
    }
}

EDIT : I just realised I said 'to keep track of the user'. Now I realized why you guys thought I wanted to do something bad with this program. I corrected it now, what I meant was 'to keep track of the user's name '.

I understand that you want to maintain a file that contains the names of all the registered users, or some other kind of current-user-independent data.

The problem

Your code tries to open the file in the current working directory of the program. Unfortunately, it depends on the way the user has launched your program.

It also ignores possible errors during the opening when reading the file. So if the file isn't there, your code will open the file as ofstream for writing (which will create the file if it doesn't exist).

How to solve it ?

To fulfill your requirements, you should open the file in a predetermined location (for example fixed during the installation process, or in the program's configuration). See this article , on where to ideally store data and configuration files on windows platform.

If you want to make sure that the program only opens the file if it already exists, you should verify the result of the open on the ifstream and issue an error message if this failed:

example.open("Name.txt");
if (!example) {
    cout << "OUCH ! Fatal error: the registration file couldn't be opened !" <<endl; 
    exit (1);     
}

How to protect the file against users ?

Note however that if your program reads and writes data from the file, the user could find it also and edit it manually. This will be difficult to prevent.

Alternatively you could consider using the windows registry , which is less trivial for the user to edit (although not impossible). The major inconvenience of this approach is that it's system dependent and it will make the porting of your code to other platforms much more difficult.

If you want to fully protect your file, you could as suggested by Chris in the comment, encrypt the file. Encryption is complex business; Consider using a library such as openssl or a proven algorithm .

This will protect you against ordinary users. But you'd still be exposed to hackers able to reverse engineer your code and to find the encryption key that must be somehow embedded in your code to decrypt the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM