2013-03-05 89 views
0

我有关于VC++ 问题如果u编译此代码在VC:VC++与G ++,COUT

#include "stdafx.h" 
#include <stdlib.h> 
//#include <stdio.h> 
#include <iostream> 
#include <Windows.h> 

TCHAR lpBuffer[MAX_PATH]; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DWORD dwBufferLength = 0; 
    if(!(dwBufferLength = GetWindowsDirectory(lpBuffer, MAX_PATH))) 
     std::cout << "Last error : "<< GetLastError() << std::endl; 
    else{ 
     std::cout << lpBuffer << std::endl; 
     /*for(DWORD i = 0; i < dwBufferLength; i++) 
      printf("%c", lpBuffer);*/ 
     std::cout << std::endl; 
    } 

    system("PAUSE"); 
    return 0; 
} 

我看到只有 “C”,如果我用g编译它++我会看到“C: \ Windows“有什么问题? 相信我应该删除 “下克++ :)

和变化” _tmain第一行的 “#包括 ”stdafx“” 到 “主”^__^

+0

G ++是否将'_tmain'函数识别为启动函数? – 2013-03-05 00:34:38

+0

nope,我应该写'main' :)谢谢你我会改变它在我的问题:) – 2013-03-05 00:36:04

+1

你正在发送一个宽字符串,一个Unicode字符串,以cout。您应该使用wcout。 G ++不是很宽。您可以使用Project + Properties,General,Character set = Multi-Byte将时钟恢复到20世纪80年代。 – 2013-03-05 01:06:49

回答

1

校正代码之后:

#include <iostream> 
#include <Windows.h> 

int main() { 
    char lpBuffer[MAX_PATH]; 
    DWORD dwBufferLength = 0; 

    if(!(dwBufferLength = GetWindowsDirectory(lpBuffer, MAX_PATH))) 
     std::cout << "Last error : "<< GetLastError() << std::endl; 
    else 
     std::cout << lpBuffer << "\n"; 
    return 0; 
} 

我用VC++(2012)和gcc 4.7.2(MinGW)得到了相同的结果(“C:\ windows”)。

+0

你应该添加'#include“stdafx”'因为它适用于VC++,我没有看到“C”,我看到“00ABF608”!!!!!!!! – 2013-03-05 00:46:54

+0

我有VC++ 2010 Express !!!!! ;) – 2013-03-05 00:53:07

+0

@LaVloZ不,你不需要包含* stdafx.h *在VC下编译;您可以转到项目属性并关闭编译器设置下的预编译头选项。杰里,也许你应该明确调用'GetWindowsDirectoryA',因为你没有使用任何TCHAR的东西。即使OP定义了_UNICODE,这也会使代码正常工作。 – Praetorian 2013-03-05 01:11:50