So this makes no sense / Official Proof C++ is broken?

First of all, this is done in netbeans on a virtual lunix 32 bit machine. Here are the two functions I have written...

Here is the working version of my function. It correctly returns true when a folder is a file.

EDIT: Nevermind; is also eratic and sometimes returns everything or nothing as a folder. BUT the commented out code still effects the answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isFolder(char * filename)
{
    struct stat t;
    lstat(filename, &t);
    if (S_ISDIR(t.st_mode))
        return true;
    else
        return false;

 
    struct stat st;
    lstat(filename, &st);
    return (S_ISDIR(st.st_mode));
}


Now I present you the non-functional version, that almost never works, except sometimes in very rare cases. And when the file is . or ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isFolder(char * filename)
{
    struct stat t;
    lstat(filename, &t);
    if (S_ISDIR(t.st_mode))
        return true;
    else
        return false;

 
//    struct stat st;
//    lstat(filename, &st);
//    return (S_ISDIR(st.st_mode));
}


If you look carefully you'll notice that the only difference is that I commented out the code that is never called. I have tested and made sure that it is never called. However, it will not work unless this part is un-commented. Pardon my french.. but what the shit? How am I to program in a language that doesn't actually.. well.. WORK
Last edited on
> It correctly returns true when a folder is a file.
¿what?


¿May you provide a testcase ( http://www.eelis.net/iso-c++/testcase.xhtml )?
I will try and do a test case when I have time.

The function is intended to tell me if a folder is a directory or a file. The problem is that the output is different depending on if the code that is never run is commented out or not. That's a core logical problem.
I would step through it with a debugger. I doubt the commented code is actually a problem.
My guess is that you are passing an invalid path name and lstat is failing. Then S_ISDIR() is checking whatever value happens to be in t at the time.

Try this:
1
2
3
4
5
6
7
8
#include <errno.h>
...
if (lstat(filename, &t)) {
    char buf[100];
    strerror_r(errno, buf, sizeof(buf));
    printf("lstat(%s) failed: %s", filename, buf);
    return false;
}

Topic archived. No new replies allowed.