views:

858

answers:

7

First of all, i'd to establish that i do have the text file in my Folders directory. Im using visual studio and it is where my source code is compiling.

The code below should demonstate why its not working. In visual studio.

int main( const int argc, const char **argv )
{
    char usrMenuOption;
    const char *cFileName = argv[ 1 ];
    checkName( cFileName );  // supplying the checkName function with contents of argv[1]

    usrMenuOption = getUsrOption();  // calling another function

    fgetc(stdin);
         return 0;
}
ifstream *openInputFile( const char *cFileName )
{
    // this function might be the pronblem.
    ifstream *inFile;
    inFile = new ifstream; 
    inFile->open( cFileName, ios::in );
    return inFile;
}
bool checkName( const char *cFileName )
{
    // it works fine if i use a regular ifstream obj and not the one from the function
    ifstream *inFile;
    inFile = openInputFile( cFileName );
    inFile->open( cFileName, ios::in );

    if ( inFile->good() )
    { 
        return true;
    }
    else
    { 
        cout << '"' << cFileName << '"' << ": File does not exist! " << endl;
        return false;
    }         
}

It does work if i use a non-pointer object for the ifstream. however i need to open all of my input files this way, using the function i made. I'm a little confused because i did not have this issue compiling in dev-cpp

+3  A: 

That's a poor way to test for existence: because if the file is open by another process, then the file exists but you can't open it.

A better way to test might be to use the GetFileAttributes Function: if it doesn't return INVALID_FILE_ATTRIBUTES then the file exists.

ChrisW
+9  A: 

You have a few options:

  1. The one you've tried - opening the file.
  2. Using stat.
  3. Using GetFileAttributes.
  4. Using FindFirstFile.

The only way to guarantee that it exists and that you can use it is to open it. If you use other methods you end up with a race condition (because the file could be deleted or locked after you check to see if it exists.

EDIT: You have a couple of other issues in your code. Firstly, you allocate a infile via new, but you never delete it. Secondly, you call open twice.

OJ
+1. Just try opening the file -- that will either work or fail. Don't even bother using a separate checkName() function, get rid of it.
j_random_hacker
+1  A: 

To check for the existence of a file (POSIX.1 compliant):

#include <unistd.h>

if (! access (file_name, F_OK))
{
  // File exists.
}
Paul Beckingham
A: 

How do i check if a file exists using ANSI C++?

#include <fstream>

inline bool FileExists(const char * filename)
{
    return std::ifstream(filename);
}
uzbones
+1  A: 

You're trying to open the file twice inside checkName(): the first time in the constructor call inside the call to openInputFile(), the second time inside checkName() itself. Why the second call to open()?

I don't know what happens when an ifstream with an already-open file attempts to open() another file, but it won't be good, and it may well depend on the exact library implementation (hence the different behaviour between Dev-C++ and MSVC++). In short, don't do it.

There is at least one other bug: You aren't closing inFile anywhere inside checkName().

But Anyway, Do This Instead

Really, it's better not to have a separate checkName() function -- just have openInputFile() attempt to open the file, and if it fails, report the error right there and/or return a NULL pointer (or even throw an exception). That way, the operation is "atomic" -- as things stand, if the file exists at the time checkName() is called but is deleted before a subsequent call to openInputFile(), your code will get very confused.

j_random_hacker
+3  A: 

If you don't mind using Boost, there is a simple function boost::filesystem::exists( path ) that would be useful to you I guess !

Benoît
A: 

I always check ifs.is_open() where ifs is a ifstream.

milan1612