我的程序應該使用隱藏文本文件來跟蹤用戶的名稱。但是,當程序啓動時,如果在同一個目錄中找不到'Name.txt'文件,它會生成一個對用戶可見的文件。這意味着用戶可以查看它,修改它等等。我怎樣才能防止這種情況發生,只有我的程序可以修改文件?另外,有沒有更好的方法來保持用戶名稱的知識(請記住,我是一般的編程新手,不僅僅在C++世界中)?如何製作無需顯示即可使用.txt文件的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);
}
}
編輯:我才意識到我說:「來跟蹤用戶的」笑我現在知道爲什麼你們認爲我想做一些不好用此程序。我現在糾正了它,我的意思是「跟蹤用戶的姓名」。
*「這意味着用戶可以查看它,修改等...「*爲什麼會出現這樣的問題? –
是你的電腦還是用戶的? –
不,我不想用它做任何事情,它只是它對我來說很愚蠢。嚴肅的軟件不會顯示廢話,它不是用戶友好的,我希望保持程序緊湊,在一個exe –