2011-01-13 135 views
1

這是正確的方法嗎?我已經嘗試了很多方法,似乎不正常如果不能正常工作

CString result; 
result = ExecuteExternalProgram(L"c.txt", L"t.txt"); // return a CString 

if (result == _T("one")) 
    MessageBox(NULL,result.GetBuffer(), L"one", MB_OK); 
else 
     MessageBox(NULL,result.GetBuffer(), L"two", MB_OK); 
+0

即使結果是「one」,它也不會進入第一個條件。它總是跳轉到ELSE。 – karikari 2011-01-13 11:24:24

回答

1

C字符串不能==相比的工作 - ==比較結果指針是否是一樣的指針,你翻譯的字符串。他們不會平等。你爲什麼不使用STRCMP

if (0 == strcmp(result, _T("one")) /* ... */ else /* ... */ 
0

並檢查是否正常工作:如果你要使用C字符串,那麼你應該使用C字符串比較函數?

0

看起來像你使用MFC的CString,它有一個重載'='運算符,所以你的條件部分是好的。但是,您需要更改這樣的代碼:

CString result; 
result = ExecuteExternalProgram(L"c.txt", L"t.txt"); // return a CString 

if (result == CString("one")) 
    MessageBox(NULL,result.GetBuffer(), L"one", MB_OK); 
else 
    MessageBox(NULL,result.GetBuffer(), L"two", MB_OK);