标签归档: windows

clock()函数的一个坑

clock_t s = clock();

Sleep(5*1000);

printf("%d\n", clock() - s);

这段代码在Windows上大于或等于5秒,但是在Linux上就小于5秒。

查资料发现 标准的应该是 Returns the processor time consumed by the program. 返回程序消耗的处理器时间。

但在MSDN上 Calculates the wall-clock time used by the calling process. 计算调用进程使用的挂钟时间,

注释写的是  The clock function tells how much wall-clock time has passed since the CRT initialization during process start. Note that this function does not strictly conform to ISO C, which specifies net CPU time as the return value. To obtain CPU times, use the Win32 GetProcessTimes function. To determine the elapsed time in seconds, divide the value returned by the clock function by the macro CLOCKS_PER_SEC. 

在Linux上是占用CPU时间,在Windows上是运行时间。所以跨平台的时候就要注意了,不要使用这个函数来做计时。

参考 http://www.51testing.com/html/00/n-3706800.html

windows获取MIME类型的例子

[cpp]
#include <stdio.h>
#include <Windows.h>

char *get_mime_type(const char *sExtension)
{
HKEY hItem;
if(!sExtension) return NULL;
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, sExtension, 0, KEY_READ, &hItem) == ERROR_SUCCESS)
{
char *sPath = (char*)malloc(MAX_PATH);
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if (RegQueryValueEx(hItem, "Content Type", NULL, &dwType, (LPBYTE)sPath, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hItem);
return sPath;
}
RegCloseKey(hItem);
}
return NULL;
}

int main()
{
char *mime = get_mime_type(".jpg");
printf("%sn", mime);

if(mime) free(mime);
return 0;
}
[/cpp]
通过查询注册表来查询 MIME。