1

The directory I wish to check is just the standard project folder from Microsoft Visual C++ where the files are written/read from by default

Below is what I have already, it prompts a user to enter a file name. Ultimately I want to be able to send this file over sockets but at the moment I am just looking to check the directory for that filename and then add it's extension on for sending.

printf("Please enter the name of the file you wish to send.\n\n");
        string inputFile;
        std::cin >> inputFile;

        /*ofstream myfile;
        myfile.open("example.txt");
        myfile << "Writing this to a file.\n";
        ifstream myfile ("example.txt");
        if (myfile.is_open())
        {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
         myfile.close();
        }
        myfile.close();
        */

        ofstream myfile;
        myfile.open ("example.txt");
        myfile << "Writing this to a file.\n";
        myfile.close();


        /*ifstream my_file(inputFile+".*");
        if (my_file.good())
        {
            printf("Success");
        }*/
    }
        /*
        if (inputFile == "example")
        {

            inputFile = example.txt;
            AESFileEncrypt(inputFile.c_str(), HKab);

            AESFileDecrypt(HKab);

            string STRING;
            ifstream infile;

            infile.open("example_aes.txt");
            int a=0;
            string previousLine="";
            while(a<1) // To get you all the lines.
            {
                getline(infile,STRING); // Saves the line in STRING.
                if (STRING != previousLine)
                {
                    previousLine=STRING;
                    cout<<STRING<<endl; // Prints our STRING.
                }



                infile.close();
                system ("pause");
            }

            //connectedSocket->SendFile("AES"+inputFile);

        }

        else if (inputFile == "liverpool")
        {
            inputFile = liverpool.jpg;
            AESFileEncrypt(inputFile.c_str(), HKab);
            AESFileDecrypt(HKab);


        }

    }
    else
    {
        printf("You have not selected a valid option.\n\n");
    }
    */

}
pnuts
  • 58,317
  • 11
  • 87
  • 139
cico_dev
  • 87
  • 2
  • 13
  • 1
    http://stackoverflow.com/questions/3828835/how-can-we-check-if-a-file-exists-or-not-using-win32-program – 001 Mar 27 '14 at 14:39
  • It is better to remove the comments from your code to make it cleaner. – Nejat Mar 27 '14 at 14:42

1 Answers1

0

If the open function of the ifstream returns true then the file exists.

You can also check file existence by:

    ifstream f("example.txt");
    if (f.good()) {
        //file exists
    } else {
        //file does not exist
    }

or

    if (access( "example.txt", F_OK ) != -1  )
    {
        //file exists
    } else {
        //file does not exist
    }

or

     struct stat buffer; 
     if (stat ("example.txt", &buffer) == 0 )
     {
         //file exists
     } else {
         //file does not exist
     }
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • The converse is not necessarily true however - opening the file may fail for other reasons, even if the file exists. – Paul R Mar 27 '14 at 14:41
  • It appears the OP doesn't necessarily know the extension, so a wildcard search is required -- "example.*". – Fred Larson Mar 27 '14 at 14:56