2015-09-08 5 views
6

L'esecuzione del seguente example for _stat from MSDN compilato con Visual C++ 2015 Express utilizzando v140_xp come Platform Toolset (target Win32) viene eseguito normalmente su Windows 7 ma non su Windows XP su diversi computer da me testati.Visual C++ 2015 express: _stat non funziona su 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 uscita:

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

Windows XP:

Problem getting information: Invalid argument 
Invalid parameter to _stat. 

E sì crt_stat.c si trova nella directory di file eseguibile che è anche il CWD.

È un errore o mi manca qualcosa?

+1

Puoi testare il vostro esempio con un percorso assoluto? –

+0

Appena testato con il percorso assoluto "D: \\ crt_stat.c". I risultati sono gli stessi. Win7 va bene, WinXP no. –

+4

MSDN: https://connect.microsoft.com/VisualStudio/feedback/details/1557168/wstat64-returns-1-on-xp-always - suona come un bug VS2015 – Petesh

risposta

4

Come indicato nei commenti, è un bug nel runtime. Al momento (2015-09-09) la correzione non è ancora disponibile in un aggiornamento, ma probabilmente lo sarà presto. Una soluzione alternativa è utilizzare invece GetFileAttributesEx.

+0

Sono stato in grado di scrivere uno shim adattando del codice da ReactOS che implementa stat utilizzando GetFileAttributesEx. https://github.com/mirror/reactos/blob/master/reactos/lib/sdk/crt/stdio/stat64.c –

0

Il bug è stato risolto in Visual C++ Redistributable per Visual Studio 2015 Update 1

ho risolto il problema con l'installazione di quella forma aggiornamento qui: https://www.microsoft.com/en-us/download/details.aspx?id=49984

+0

Anche update1, con link statico lib di runtime C++ (Debug multithread), la statistica continua a fallire sempre con parametro non valido – alpha