2015-09-08 11 views
6

Chạy sau example for _stat from MSDN được biên dịch bằng Visual C++ 2015 Express sử dụng v140_xpPlatform Toolset (đích Win32) chạy bình thường trên Windows 7 nhưng không chạy trên Windows XP trên một số máy tôi đã thử nghiệm.Visual C++ 2015 express: _stat không hoạt động trên Windows XP

// crt_stat.c 
// This program uses the _stat function to 
// report information about the file named crt_stat.c. 

#include <time.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <errno.h> 

int main() 
{ 
    struct _stat buf; 
    int result; 
    char timebuf[26]; 
    char* filename = "crt_stat.c"; // Absolute paths like "D:\\crt_stat.c" produce the same behaviour. 
    errno_t err; 

    // Get data associated with "crt_stat.c": 
    result = _stat(filename, &buf); 

    // Check if statistics are valid: 
    if (result != 0) 
    { 
    perror("Problem getting information"); 
    switch (errno) 
    { 
    case ENOENT: 
     printf("File %s not found.\n", filename); 
     break; 
    case EINVAL: 
     printf("Invalid parameter to _stat.\n"); 
     break; 
    default: 
     /* Should never be reached. */ 
     printf("Unexpected error in _stat.\n"); 
    } 
    } 
    else 
    { 
    // Output some of the statistics: 
    printf("File size  : %ld\n", buf.st_size); 
    printf("Drive   : %c:\n", buf.st_dev + 'A'); 
    err = ctime_s(timebuf, 26, &buf.st_mtime); 
    if (err) 
    { 
     printf("Invalid arguments to ctime_s."); 
     return 1; 
    } 
    printf("Time modified : %s", timebuf); 
    } 
} 

Windows 7 đầu ra:

File size  : 6 
Drive   : D: 
Time modified : Tue Sep 8 10:06:57 2015 

Windows XP đầu ra:

Problem getting information: Invalid argument 
Invalid parameter to _stat. 

Và vâng crt_stat.c nằm trong thư mục thực thi cũng là CWD.

Đây có phải là lỗi hoặc tôi thiếu gì đó không?

+1

Bạn có thể kiểm tra ví dụ của bạn với một đường dẫn tuyệt đối? –

+0

Chỉ cần thử nghiệm nó với đường dẫn tuyệt đối "D: \\ crt_stat.c". Các kết quả đều giống nhau. Win7 là tốt, WinXP là không. –

+4

MSDN: https://connect.microsoft.com/VisualStudio/feedback/details/1557168/wstat64-returns-1-on-xp-always - nghe giống như lỗi VS2015 – Petesh

Trả lời

4

Như được nêu trong các nhận xét, đây là bug trong thời gian chạy. Ngay bây giờ (2015-09-09) bản sửa lỗi chưa có sẵn trong bản cập nhật, nhưng có thể sẽ sớm có. Giải pháp thay thế là sử dụng GetFileAttributesEx thay thế.

+0

Tôi đã có thể viết một shim bằng cách điều chỉnh một số mã từ ReactOS thực hiện stat bằng cách sử dụng GetFileAttributesEx. https://github.com/mirror/reactos/blob/master/reactos/lib/sdk/crt/stdio/stat64.c –

0

Các lỗi được giải quyết trên Visual C++ Redistributable cho Visual Studio 2015 Update 1

tôi giải quyết vấn đề bằng cách cài đặt mà hình thức cập nhật tại đây: https://www.microsoft.com/en-us/download/details.aspx?id=49984

+0

Thậm chí update1, với static link C++ runtime lib (Multithread Debug), stat vẫn luôn thất bại với tham số không hợp lệ – alpha

Các vấn đề liên quan