我編碼應該讀出文件的一類,我有以下問題字符串將不起作用:C++比較的std ::如果我不使用代碼:: Blocks的編譯,但cygwin的
如果我在Code :: Blocks環境內編譯它,一切正常。 如果要加載的文件的第一行不同於「OFF」,它將跳轉到第二個if語句並退出程序...
但是,如果我使用g ++在cygwin中編譯,程序跳轉到第二個if語句無論文件中實際寫了什麼。 有何建議?
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
polyeder offManager::readOFF(std::string filename)
{
//Open File
std::ifstream file(filename.c_str());
// If file couldn't be opened
if(!file.is_open())
{
std::cerr << "ERROR: Konnte Datei \""
<< filename << "\" nicht oeffnen!"
<< std::endl;
exit (2);
}
// Test if the file really is an OFF File
std::string line;
std::string off ("OFF");
getline(file, line);
if (line.compare(off) != 0)
{
std::cerr << "ERROR: Datei \""
<< filename << "\" ist nicht im OFF Format!"
<< std::endl;
file.close();
exit (2);
}
...
}
如果鍵入G ++ -v在Cygwin中,我得到以下幾點: Blablabla 線程型號名稱:POSIX GCC-4.5.3版本(GCC)
代碼::塊使用此版本: 線程模型:Win32的 gcc版本4.4.1(TDM-2的mingw32)
您還沒有檢查'getline'成功。 – 2012-07-14 16:53:13
在輸出診斷中,輸出'line'的值,以便您可以看到它的內容。它不會以'\ n''結尾,是嗎?順便說一句,您應該在C++程序中#include而不是''。而你的縮排是可怕的。 –
2012-07-14 16:55:40
感謝您的幫助...我已經替換了標題...不知道爲什麼我的縮進是可怕的;) – user1525814 2012-07-14 18:15:05