user3001499
user3001499

Reputation: 811

C++ check if file exists, if yes, change output

I don't know if its relevant, but I'm using Windows 7 and Microsoft Visual Studio 2010.

All I want to do, is ask the user for a filename, check if a file of that name exists, while it does exist, ask them for a different one

My Attempt

Main.cpp

std::cout << std::endl << "You must create a username" << std::endl;
std::cin >> username;
user.checkFile(username);

User.cpp

void User::checkFile(std::string &username)
{

std::ifstream fin (username + ".txt");
    while (fin.good)
    {
    std::cout << "This username already exists, please choose another.";
    std::cin >> username;
     if (fin.bad)
        {
            break;
            fin.close();
        }
    }
}

This correctly identifies if a file of that name exists, but then even when I type a name that doesn't exist, it still tells me it does exist

Upvotes: 0

Views: 1554

Answers (6)

Remy Lebeau
Remy Lebeau

Reputation: 598134

Main.cpp

std::cout << std::endl << "You must create a username" << std::endl;
do
{
    std::cin >> username;
    if (user.checkFile(username))
        break;
    std::cout << "This username already exists, please choose another." << std::endl;
}
while (true);

User.cpp

bool User::checkFile(const std::string &username)
{
    FILE *fin = fopen((username + ".txt").c_str(), "r");
    if (fin)
    {
        fclose(fin);
        return false;
    }
    return true;
}

Upvotes: 1

Zac Howland
Zac Howland

Reputation: 15870

If you are limiting yourself to just Windows, there is an API function that does exactly that: PathFileExists.

If you want to stick with the standard library, you can do the following:

string filename;
cin >> filename;
ifstream fin(filename);
if (fin.is_open())
{
    // file opened successfully
}
else
{
    // file did not open
}

Upvotes: 1

asifgoldpk
asifgoldpk

Reputation: 174

int main(void) {

    bool fileExists = true;

    while (fileExists)
    {
            string fileName;
            cout << "Enter file name: ";
            cin >> fileName;

            ifstream ifs(fileName.c_str());

            fileExists = !ifs.good();
    }

    return 0;
}

Upvotes: 1

Martin Perry
Martin Perry

Reputation: 9527

I would use simple C function for file handling.

void User::checkFile(std::string &username)
{
    std::string username2 = username + ".txt";
    FILE * f = fopen(username2.c_str(), "r");
    while (f != NULL)
    {
      fclose(f);

      std::cout << "This username already exists, please choose another.";
      std::cin >> username;
      std::string username2 = username + ".txt";
      f = fopen(username2.c_str(), "r");          
    }
}

This way, yout variable username will hold valid name, after function cal returns, because you are passing it via reference.

Upvotes: 1

David G
David G

Reputation: 96845

After inputting the new string, you need to reopen the file with that name:

std::cout << "This username already exists, please choose another.";
std::cin >> username;

fin.close();
fin.open(username);

Also, good and bad are functions that must be called using the call operator ().

Upvotes: 0

Faisal Khalid
Faisal Khalid

Reputation: 650

I think you will need to convert your string using c_str() function. For example:

string a="df";
a.c_str(); // will return c string

Upvotes: -1

Related Questions