so, im trying to make a simple funtion to see if there is a file in a folder. been looking everywhere but havent found one that worked out. annnnnnnnnnnnnd code snippet:
`

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    hFind = FindFirstFile(_T"C:\\Dev-Cpp\\notes", &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE) 
    {
        cout << "No File found\n";
    } 
    else 
    {
        cout << "Guess I found a File\n";
    }

well, the function comes up with no errors but if the file is empty it says that it found a file. can someone tell me what am i not seeing?

Recommended Answers

All 7 Replies

sorry guys, false alarm. found the problem.

Well, you can do it using plain ifstream handling:

bool existFile(string filename){
    return ((ifstream(filename.c_str())).good());
}

Click Here

You can also do it with _stat()

Oh I would like to see that thing done with >
try except thing like, because the MSDN program still uses errror.h, and hendels the error in old fasion way!

because the MSDN program still uses errror.h, and hendels the error in old fasion way!

You mean errno.h? _stat and FindFirstFile are both C-based API functions, so errno and return codes are essentially the only option for handling errors. You can wrap your own exception handling mechanism around it if you really want to, but the "old" way doesn't mean that it's not functional or effective. ;)

#include <errno.h>
#include <exception>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

class StatException : public exception
{
    string _message;
    string _filename;
public:
    StatException(const string& message, const string& filename)
        : _message(message), _filename(filename)
    {}

    const char *what() const { return _message.c_str(); }
};

class FileStat
{
    struct _stat _buf;
public:
    FileStat(const string& filename)
    {
        if (_stat(filename.c_str(), &_buf))
        {
            switch (errno)
            {
            case ENOENT: throw StatException("File not found", filename);
            case EINVAL: throw StatException("Invalid parameter", filename);
            }
        }
    }

    _off_t Size() const { return _buf.st_size; }
};

int main()
{
    try
    {
        FileStat stat("test.txt");

        cout << "Size: " << stat.Size() << " bytes\n";
    }
    catch (const StatException& ex)
    {
        cerr << ex.what() << '\n';
    }
}

Who said that it is what youhave added, but then !

And since Decepticon undersands the try catch block, that idea how you cold have iput buffer of chars and then convert it to int, well try atoi(cInputBuffer) woudl not be to much out there.
It is like, you try to convert it, and if you can you do what you do, and if not possible you go to eror handler.
What have you gain like that, whell your app is more robust, they will not be able to input some ascii codes und stuf, but...
I think that Decepticon deserves some points or what ever is this for!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.