2012-07-14 61 views
1

我編碼應該讀出文件的一類,我有以下問題字符串將不起作用: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)

+0

您還沒有檢查'getline'成功。 – 2012-07-14 16:53:13

+0

在輸出診斷中,輸出'line'的值,以便您可以看到它的內容。它不會以'\ n''結尾,是嗎?順便說一句,您應該在C++程序中#include 而不是''。而你的縮排是可怕的。 – 2012-07-14 16:55:40

+0

感謝您的幫助...我已經替換了標題...不知道爲什麼我的縮進是可怕的;) – user1525814 2012-07-14 18:15:05

回答

0

你要打開的文件很可能在DOS文本格式,所以它的線與\r\n結束,而不僅僅是\n

Cygwin FAQ有一個條目約this issue。我將你的代碼複製到一個簡單的main驅動程序中,並通過兩種方式進行編譯。首先,正規途徑:

$ g++ s.cc 
$ ./a.exe test 
ERROR: Datei "test" ist nicht im OFF Format! 

,然後用建議的修復(文本翻譯模式打開,以二進制方式寫):

$ g++ s.cc /usr/lib/automode.o 
$ ./a.exe test 
+0

謝謝你的幫助!它會工作沒有在大多數unix環境下的建議修復,雖然(意味着它只有cygwin的依賴?)? – user1525814 2012-07-14 18:13:14

+0

@ user1525814:是的。該修復僅適用於Cygwin。 – jxh 2012-07-14 19:04:03

相關問題