Checking if a path isDir()

D

Daz

Would anyone know of a way to check whether or not a particular path is
a directory? If I have to go down the route of using _findfirst and
_findnext (windows specific), then so be it. However, I don't think it
should be that difficult to check.

My original idea was to check to see if the path can be opened with
fstream, but this can give false results if the path is a filename, and
the file cannot be opened. I am using boost, and can't see any way of
using this, or though I am pretty certain it would have been
implemented as boost is fantastic!

Any suggestions would be appreciated.

Daz
 
R

Rolf Magnus

Daz said:
Would anyone know of a way to check whether or not a particular path is
a directory?

It's likely that someone would know that. However, it is not possible in
standard C++, which doesn't offer any directory handling functionality.
 
V

Victor Bazarov

Daz said:
Would anyone know of a way to check whether or not a particular path
is a directory?

Yes, somebody in the newsgroup dedicated to programming your OS would.
"Directory" is a concept foreign to C++ _language_ (as of now, anyway).
The reason is relatively simple: C++ exists on some platforms that do
not have "directory" and requiring a

If you can tell us in the terms of the language itself what makes your
"path" a "directory", IOW, the algorithm, we can help you expressing it
in C++. If that algorithm cannot be written down in C++ terms _only_,
or if you just don't have it, we cannot help you.
If I have to go down the route of using _findfirst and
_findnext (windows specific), then so be it. However, I don't think it
should be that difficult to check.

Do you think it might be different on, say, a Mac OS versus, say, VMS?
If I can tell you how to do it on Mac OS, is it going to help?
My original idea was to check to see if the path can be opened with
fstream, but this can give false results if the path is a filename,
and the file cannot be opened. I am using boost, and can't see any
way of using this, or though I am pretty certain it would have been
implemented as boost is fantastic!

Boost has its own discussion forums, AFAIK. You should ask there.

V
 
D

Daz

I will indeed ask in the boost discussion forum, and also look into
creating a portable function (if it's even possible), that I can use
and may help others in this dilema. I think the question I need to
answer, is 'what's the difference between a filename, and a
directory?'. I am sure there are things you can only do to a directory,
that you can't do to a file and vice-verse.

V, you said:
Yes, somebody in the newsgroup dedicated to programming your OS would.
"Directory" is a concept foreign to C++ _language_ (as of now, anyway).
The reason is relatively simple: C++ exists on some platforms that do
not have "directory" and requiring a

but I can't seem to see anything after it.

Thanks for the input guys!
 
M

Markus Schoder

Daz said:
Would anyone know of a way to check whether or not a particular path is
a directory? If I have to go down the route of using _findfirst and
_findnext (windows specific), then so be it. However, I don't think it
should be that difficult to check.

My original idea was to check to see if the path can be opened with
fstream, but this can give false results if the path is a filename, and
the file cannot be opened. I am using boost, and can't see any way of
using this, or though I am pretty certain it would have been
implemented as boost is fantastic!

bool boost::filesystem::is_directory(const path &);
 
V

Victor Bazarov

Daz said:
I will indeed ask in the boost discussion forum, and also look into
creating a portable function (if it's even possible), that I can use
and may help others in this dilema. I think the question I need to
answer, is 'what's the difference between a filename, and a
directory?'. I am sure there are things you can only do to a
directory, that you can't do to a file and vice-verse.

As far as C++ is concerned, there are things called "files" (and the
connection between a C++ program and them is the "file-based streams"
and a couple of C library functions: 'remove' and 'rename'). But C++
does not know what "files" really are. They are opaque to C++. If
you need to "read" a "file", you open a stream. To identify a "file"
you use its "name". The format of the "name", how it's structured or
what mechanism is behind opening a file or removing or renaming it,
is totally unknown to C++. It does not matter to a C++ program. And
that's why it cannot be defined.
V, you said:

but I can't seem to see anything after it.

I can't really do anything about that.

V
 
D

Daz

Victor said:
I can't really do anything about that.
I was kind of hoping you would tell me the rest of the sentance, as I
figured you might have accidentally deleted it whilst typing.

I have been doing some thinking...


//---------/ CODE START /----------//

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

using namespace std;
const char dirString[] = "C:/";
const char fileString[] = "C:/test.txt";

int test1()
{
/*
Check to see if a file open for writing can
be read from.
*/
ofstream write(fileString, ios::eek:ut);
if (!write)
cout << "Could not open file to write!" << endl;
else
write << "Test\n";
ifstream read(fileString, ios::in);
if (!read)
{
cout << "Could not open file to read from!" << endl;
return 0;
}
if (read)
{
cout << "fstream can read from a file already"
<< " opened for writing!" << endl;
std::string line;
read.seekg(-4);
getline(read, line, '\n');
cout << line << "read from file" << endl;
return 1;
}
return 0;
}
int test2()
{
/*
Check to see if fstream can open a directory.
*/
fstream inDir(dirString);
if (inDir)
{
cout << "Directory opened with fstream\n" << endl;
cout << "Crap! There goes my theory...\n" << endl;
return 0;
}
else if (!inDir)
{
cout << "fstream cannot open directories!\n" << endl;
return 1;
}
return 0;
}
int main()
{
signed int test1Result = test1();
signed int test2Result = test2();
if (test1Result == 1 && test2Result == 1)
{
cout
<< "As we can't open a directory with fstream,\n"
<< "but can read from a file if it exists and\n"
<< "is open for writing, can we not use this\n"
<< "to be give a 100% correct answer as to\n"
<< "whether or not the given path is a file or\n"
<< "a directory?" << endl;
}
return 0;
}

//----------/ CODE END /-----------//

What's interesting is that you can't actually 'read' data from the file
when it's opened for writing, however, fstream appears to open the
file. To double-check I wrote a tiny proggy that opened the file in
advance, and kept it open until I pressed a key, and I still got the
same results from this program.

If I were to combine the two tests so that if the 'path name' exists
(which boost::filesystem::exists() will tell me. if the file can be
opened for reading, it's a file, of not, then it's a directory?

I hope this makes sense, and I would be happy to hear some feedback
from everyone. Please don't pick at the code quality. I am not an
expert and I am still learning things, so it may not be as secure as it
could be, but it was only designed for demonstration purposes.

Best wishes

Daz
 
D

Daz

Markus said:
bool boost::filesystem::is_directory(const path &);

Haha! I just spent ages working on my example... That'll teach me to
read the next posts before pointlessly posting my code that I spent
time doing and didn't need to...

Thanks again Markus! :)
 
I

Ian Collins

Daz said:
If I were to combine the two tests so that if the 'path name' exists
(which boost::filesystem::exists() will tell me. if the file can be
opened for reading, it's a file, of not, then it's a directory?
Assuming the environment supports the concept, you simply might not have
the necessary permissions to open the file.

You have to resort to platform specifics to determine the 'type' of a file.
 
D

Daz

Ian said:
Assuming the environment supports the concept, you simply might not have
the necessary permissions to open the file.

You have to resort to platform specifics to determine the 'type' of a file.

Unfortunately, that's very true... Thanks for pointing that out,
though. Much appreciated.
 
L

Larry I Smith

Daz said:
Would anyone know of a way to check whether or not a particular path is
a directory? If I have to go down the route of using _findfirst and
_findnext (windows specific), then so be it. However, I don't think it
should be that difficult to check.

My original idea was to check to see if the path can be opened with
fstream, but this can give false results if the path is a filename, and
the file cannot be opened. I am using boost, and can't see any way of
using this, or though I am pretty certain it would have been
implemented as boost is fantastic!

Any suggestions would be appreciated.

Daz

Check the docs for 'stat()' (called '_stat()' on Windows).
Here's the Windows doc (including examples):

http://msdn2.microsoft.com/en-us/library/14h5k7ff.aspx

For unix/linux see 'man -S 2 stat'.

Read the paragraph concerning the 'st_mode' field of the
struct returned by stat(); by OR'ing that field with
_S_IFDIR, you can tell if the pathname passed to stat()
is, or is not, a directory.
 
D

Daz

Larry said:
Check the docs for 'stat()' (called '_stat()' on Windows).
Here's the Windows doc (including examples):

http://msdn2.microsoft.com/en-us/library/14h5k7ff.aspx

For unix/linux see 'man -S 2 stat'.

Read the paragraph concerning the 'st_mode' field of the
struct returned by stat(); by OR'ing that field with
_S_IFDIR, you can tell if the pathname passed to stat()
is, or is not, a directory.

Thanks for that Larry! The information is most useful. However, as
boost already supports such a query ( boost::filesystem::is_directory()
), I would only be reinventing the wheel. Plus, I doubt that I would
EVER be able to make my system work half as well as the wonderful
people who code boost have managed to do so.

The link is greatly received, though. It helps increase my
understanding of how certain things work within c++, which is never a
bad thing.

Thanks again! :)

Daz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top