Bugsia
Bugsia

Reputation: 21

fstream doesnt write and make files

In have a problem in my Code. I wanna use fstream to make files and also to write to files, but it does only make one File and doesnt write anything to it and the other two arent made. I hope somebody can help me. The PlayerMake ofstream file is made and the other two arent made.

std::string line;

        std::ifstream nameFree;
        nameFree.open(PlayerList);

        if (nameFree.is_open()) {
            while (std::getline(nameFree, line)) {
                if (line == name) {
                    std::cout << "Dieser Name ist leider bereits belegt. Bitte suche dir einen anderen aus." << std::endl;
                    nameFree.close();
                    Neu();
                }
                else {
                    std::string FileCreateCPP = "D:/Doucments/Game/" + name + ".cpp";
                    std::string FileCreateTXT = "D:/Doucments/Game/" + name + ".txt";

                    nameFree.close();

                    std::ofstream PlayerMake;
                    PlayerMake.open(PlayerList, std::ios::app);

                    std::ofstream cpp;
                    cpp.open(FileCreateCPP);

                    std::ofstream txt;
                    txt.open(FileCreateTXT);

                    if (cpp.is_open() && txt.is_open()) {
                        cpp << name;
                        PlayerMake << name << std::endl;
                        PlayerMake.close();
                        cpp.close();
                        txt.close();
                    }
                    else {
                        std::cerr << "Fehler beim erstellen deines Kontos 1" << std::endl;
                        cpp.close();
                        txt.close();
                        Neu();
                    }
                }
            }
        }
        else {
            std::cerr << "Fehler beim erstellen deines Kontos 2" << std::endl;
            Neu();
        }
    }

I did some testing now and made my Code a little bit better and recognized, that the code, with std::cout << "Hallo" << std::endl; doesnt get runned.

    int Free = 0;
    std::string name;

    std::cout << "Hi. Bitte gebe deinen Namen für den neuen Spielstand ein und bestaetige es mit Enter. Falls du wieder zurück möchtest, gebe 'Exit' ein und bestaetige es ebenfalls mit Enter." << std::endl;
    std::cin >> name;

    if (name == "Exit") {
        main();
    }
    else {
        std::string line;

        std::ifstream nameFree;
        nameFree.open(PlayerList);

        if (nameFree.is_open()) {
            while (std::getline(nameFree, line)) {
                if (line == name) {
                    std::cout << "Dieser Name ist leider bereits belegt. Bitte suche dir einen anderen aus." << std::endl;
                    Free == 0;
                    Neu();
                }
                else {
                    Free == 1;
                }
            }

            nameFree.close();

            if (Free == 1) {
                std::cout << "Hallo" << std::endl;

                std::string FileCreateCPP = "D:/Doucments/Game/Players/" + name + ".cpp";
                std::string FileCreateTXT = "D:/Doucments/Game/" + name + ".txt";

                std::ofstream PlayerMake;
                PlayerMake.open(PlayerList, std::ios::app);

                std::ofstream cpp;
                cpp.open(FileCreateCPP);

                std::ofstream txt;
                txt.open(FileCreateTXT);

                if (cpp.is_open() && txt.is_open()) {
                    cpp << name;
                    PlayerMake << name << std::endl;
                    PlayerMake.close();
                    cpp.close();
                    txt.close();
                }
                else {
                    std::cerr << "Fehler beim erstellen deines Kontos 1" << std::endl;
                    cpp.close();
                    txt.close();
                    Neu();
                }
            }
        }
        else {
            std::cerr << "Fehler beim erstellen deines Kontos 2" << std::endl;
            Neu();
        }
    }
}

Here is code you are able to run:

#include <iostream>
#include <string>
#include <fstream>

std::string PlayerList = "C:/PlayerList.txt";


void Neu() {
    std::string name;

    std::cout << "Hi. Bitte gebe deinen Namen für den neuen Spielstand ein und bestaetige es mit Enter. Falls du wieder zurück möchtest, gebe 'Exit' ein und bestaetige es ebenfalls mit Enter." << std::endl;
    std::cin >> name;

    if (name == "Exit") {

    }
    else {
        std::string line;

        std::ifstream nameFree;
        nameFree.open(PlayerList);

        if (nameFree.is_open()) {
            while (std::getline(nameFree, line)) {
                if (line == name) {
                    std::cout << "Dieser Name ist leider bereits belegt. Bitte suche dir einen anderen aus." << std::endl;
                    nameFree.close();
                    Neu();
                }
                else {
                    std::string FileCreateCPP = "D:/Doucments/Game/" + name + ".cpp";
                    std::string FileCreateTXT = "D:/Doucments/Game/" + name + ".txt";

                    nameFree.close();

                    std::ofstream PlayerMake;
                    PlayerMake.open(PlayerList, std::ios::app);

                    std::ofstream cpp;
                    cpp.open(FileCreateCPP);

                    std::ofstream txt;
                    txt.open(FileCreateTXT);

                    if (cpp.is_open() && txt.is_open()) {
                        cpp << name;
                        PlayerMake << name << std::endl;
                        PlayerMake.close();
                        cpp.close();
                        txt.close();
                    }
                    else {
                        std::cerr << "Fehler beim erstellen deines Kontos 1" << std::endl;
                        cpp.close();
                        txt.close();
                        Neu();
                    }
                }
            }
        }
        else {
            std::cerr << "Fehler beim erstellen deines Kontos 2" << std::endl;
            Neu();
        }
    }
} 

Upvotes: 0

Views: 108

Answers (1)

001
001

Reputation: 13533

As far as I can tell the code is trying to create a new user. You check the PlayerList to see if the name exists - except you only check the first name in the list because of the else.

Note in my code I never have to call close(). The destructors will do that.

// Adding a helper function to simplify
bool nameExists(std::string name, std::string path)
{
    std::string line;
    std::ifstream nameFree(path);
    if (nameFree) {
        while (std::getline(nameFree, line)) {
            if (line == name) {
                return true;
            }
        }
    }
    return false;
}

// Returns false if user enters "Exit" or file error
bool Neu(std::string path)
{
    std::string name;

    std::cout << "Hi. Bitte gebe deinen Namen für den neuen Spielstand ein und bestaetige es mit Enter. Falls du wieder zurück möchtest, gebe 'Exit' ein und bestaetige es ebenfalls mit Enter." << std::endl;
    std::cin >> name;

    if (name == "Exit") {
        return false;
    }
    // Check if name already exists...
    if (nameExists(name, path)) {
        // It does. Try again
        std::cout << "Dieser Name ist leider bereits belegt. Bitte suche dir einen anderen aus." << std::endl;
        return Neu(path);
    }
    else {
        // It does not exist. Add it.
        std::string FileCreateCPP = "D:/Doucments/Game/" + name + ".cpp";
        std::string FileCreateTXT = "D:/Doucments/Game/" + name + ".txt";

        // TODO: make sure name does not contain any characters
        // that are not allowed by OS in file names

        std::ofstream PlayerMake(path, std::ios::app);
        std::ofstream cpp(FileCreateCPP);
        std::ofstream txt(FileCreateTXT);

        if (PlayerMake && cpp && txt) {
            cpp << name;
            PlayerMake << name << std::endl;
            return true;
        }
    }
    return false;
}

int main()
{
    while (Neu("C:/PlayerList.txt")) {
        std::cout << "Player created\n";
    }
}

Upvotes: 0

Related Questions