作者:朱金燦
來源:http://www.cnblogs.com/clever101
請看下面一段代碼:
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main( void )
{
struct _stat buf;
int result;
char timebuf[26];
char* filename = "F:\\MyTest\\MyTest\\src\\TestUnix\\stdafx.h";
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 );
}
getchar();
return 0;
}
熟悉unix或linux平台開發的朋友可能以為它是在unix或linux平台開發的。實際上它是MSDN Library for Visual Studio 2005上的一個例子,可以在VS C++ 2005上成功編譯(多位元組字元集編譯)。
開始我也不太明白為何Windows系統有unix的標頭檔。最近看了《Windows作業系統原理》才徹底明白了。究其原因,Windows 2000/XP有三種環境子系統:POSIX/UNIX、OS/2和Win32(OS/2隻能用於x86系統)。因為有了POSIX/UNIX這個子系統,自然也就有了一個UNIX SDK,所以出現上面的標頭檔就不足為奇了。事實上微軟的發展和unix系統是有一些淵源的:微軟曆史上曾推出過unix作業系統;windows系統的很多核心開發人員都是資深的unix作業系統的設計和開發人員。
那麼這個POSIX/UNIX這個子系統有什麼用呢?POSIX代表了UNIX類型的作業系統的國際標準集,它鼓勵製造商實現相容的UNIX風格介面,以使編程者很容易地將他們的應用程式從一個系統移到另一個系統。正因為具有這個特點,很多軟體底層(想實現跨平台)、跨平台庫喜歡調用UNIX SDK中的函數。我在著名的開源映像庫GDAL就見過UNIX SDK中的函數。不過Windows 2000/XP只實現了POSIX.1標準(ISO/IEC 9945-11990或IEEE POSIX 1003.1-1990)。所需的POSIX一致性文檔位於Platform SDK中的"HELP目錄中。
參考文獻:
1. MSDN Library for Visual Studio 2005,Microsoft Corporation
2. 《Windows作業系統原理》,尤晉元 史美林 陳向群 向勇 王雷 馬洪兵 鄭扣根 馬洪兵 編著