2012-04-10 26 views
0

我創建了一個簡單的代碼:STAT()在MinGW的工作錯誤

int main(int argc, char *argv[]) 
{ 
    struct stat eStat; 
    int result; 
    struct stat eStat2; 
    int result2; 

    result = stat("g:/temp/dvd", &eStat); 
    printf("result=%d | eStat.st_mode=%d | S_IFMT=%d | S_IFDIR=%d\n",result,eStat.st_mode,S_IFMT,S_IFDIR); 

    if((eStat.st_mode & S_IFMT) == S_IFDIR) 
     printf("It is a dir!!!\n"); 
    else 
     printf("not a dir\n"); 

    result2 = stat("g:\\temp\\test.txt", &eStat2); 
    printf("test.txt result2=%d | eStat2.st_mode=%d | S_IFMT=%d | S_IFDIR=%d\n",result2,eStat2.st_mode,S_IFMT,S_IFDIR); 

    return 0; 
} 

我在VS2010(Windows7, C++)編譯此代碼,輸出:

result=0 | eStat.st_mode=16895 | S_IFMT=61440 | S_IFDIR=16384    
It is a dir!!!      
test.txt result2=0 | eStat2.st_mode=33206 | S_IFMT=61440 | S_IFDIR=16384 

Linux(Debian stable, gcc)編譯此代碼,輸出:

result=0 | eStat.st_mode=16877 | S_IFMT=61440 | S_IFDIR=16384  
It is a dir!!!     
test.txt result2=0 | eStat2.st_mode=33188 | S_IFMT=61440 | S_IFDIR=16384 

當我在mingw(gcc) on Windows7編譯時,輸出:

result=0 | eStat.st_mode=6 | S_IFMT=61440 | S_IFDIR=16384   
not a dir     
test.txt result2=0 | eStat2.st_mode=6 | S_IFMT=61440 | S_IFDIR=16384 

爲什麼st_mode始終顯示6,當我在mingw編譯?

+0

對於使用mingw的Dev C++,適合我。什麼是編譯器版本?你在使用任何IDE嗎? – 2012-04-10 09:53:43

+0

我使用cmake 2.8.7版和mingw gcc-core-4.6.1-2進行編譯。你可以用mingw gcc編譯嗎?我沒有使用IDE。我嘗試在Windows上編譯genisoimage。 – 2012-04-10 10:03:04

+0

我從[這裏](http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/)下載了mingw,它對我來說完全正常 – 2012-04-10 10:19:12

回答

1

您更改了哪些頭文件使stat()MinGW下正常工作?我懷疑結果我從測試

stat(path, &stbuf) == -1 

得到,但因爲我在一個遞歸目錄下降使用它,我不能肯定,我不知道測試的可訪問性是(1)正確那點在下降,或者; (2)檢索正確的值以檢查可訪問性。