保存用户设置的程序的指针

时间:2016-05-11 06:11:43

标签: c++ xcode save settings fstream

我大约一周前完成了第二个学期的编程。我们在一年中学到了很多,但还没有讨论构建保存用户设置的应用程序,但我们确实讨论了文件变量,因此我决定尝试构建一个将用户设置保存到文件的程序。
我已经运行程序并将设置保存到.txt文件(在Mac上使用Xcode),但这是我第一次这样做

我确信在做这件事时我犯了一些错误。我想要一些关于如何改进它的指针,可能更优雅的方式,或更传统的方式。我确定我创建文件夹的方法不正确。我也不确定如何对通道进行编码以便它知道计算机的用户名,我只是对我的通道进行了硬编码(为了在线发布而省略)。

程序本身是微不足道的,它只是构建一个盒子,我只是想构建一个保存设置的应用程序,我并不担心程序正在做什么。

#include <iostream>
#include <fstream>
#include <string.h>//for functions in date function
#include <time.h> //for functions in date function
#include <sys/stat.h>//for mkdir functions
using namespace std;

int EmptyFileChecker(ifstream &FI, const char *P);
void RetrievePreviousSettings(ifstream &FI, char &BS, int &H, int &W, const char *P);
void EnterNewSettings(char &BS, int &H, int &W);
void BuildBox(const char &BS, const int &H, const int &W);
void SaveSettings(ofstream &FO, const char &BS, const int &H, const int &W, const char *P);
string Date();

int main()
{
    int Heighth=0;
    int Width=0;
    char BoxSymbol = '*';

    int UserChoice=0;//for making new settings or not
    int SaveSettingsFlag = 0;

    const char *Path = "/Users/MYUSERNAME/Library/Application Support/The Lyons' Den/TheLyons'DenBoxSettings.txt";//code variable for username

    ifstream FileIn;
    ofstream FileOut;

    mkdir("/Users/MYUSERNAME/Library/Application Support/The Lyons' Den", ACCESSPERMS);//MODE??

    if (EmptyFileChecker(FileIn, Path))
    {
        cout << "Previous settings detected.\n";
        cout << "Enter 1 to use old settings, 2 to enter new: ";

        cin >> UserChoice;

        if (UserChoice == 1)
            RetrievePreviousSettings(FileIn, BoxSymbol, Heighth, Width, Path);

        else
        {
            EnterNewSettings(BoxSymbol, Heighth, Width);
            SaveSettingsFlag = 1;
        }
    }

    else
    {
        EnterNewSettings(BoxSymbol, Heighth, Width);
        SaveSettingsFlag = 1;
    }

    BuildBox(BoxSymbol, Heighth, Width);

    if (SaveSettingsFlag)// only save new settings if new settings were entered
        SaveSettings(FileOut, BoxSymbol, Heighth, Width, Path);
}

int EmptyFileChecker(ifstream &FI, const char *P)
{
    FI.open(P);

    if (FI.fail())
        return 0;

    else if (FI.eof())
        return 0;

    else
        return 1;
}

void RetrievePreviousSettings(ifstream &FI, char &BS, int &H, int &W, const char *P)
{
    FI >> H;
    FI >> W;
    FI >> BS;

    cout << "\nHeighth: " << H << endl;
    cout << "Width: " << W << endl;
    cout << "Box Symbol: " << BS << endl;
}

void EnterNewSettings(char &BS, int &H, int &W)
{
    cout << "\nHeighth: ";

    cin >> H;

    cout << "\nWidth: ";

    cin >> W;

    cout << "\nBox Symbol: ";

    cin >> BS;
}

void BuildBox(const char &BS, const int &H, const int &W)
{
    cout << endl;

    for (int i = 1; i <= H; i++)
    {
        for (int i = 1; i <= W; i++)
            cout << BS << " ";

        cout << endl;
    }

    cout << endl;
}

void SaveSettings(ofstream &FO, const char &BS, const int &H, const int &W, const char *P)
{
    FO.open(P);

    if (FO.fail())
        cout << "Couldn't Open File\n";

    FO << H  << endl;
    FO << W  << endl;
    FO << BS << endl;

    FO << "Date of Settings: " << Date() << endl;
    FO << "Do not modify these settings manually";

    FO.close();
}

string Date()//not my code here - just modified it to read easier
{
    char Time[50];
    time_t now = time(NULL);
    strftime(Time, 50, "%b, %d, %Y", localtime(&now)); //short month name
    return string(Time);
}

0 个答案:

没有答案