2014-03-25 33 views
-2

我已經給了一個賦值,我需要從一個名爲「filename.s」的文件中獲取一些輸入,我的代碼應該將輸出寫入名爲「filename.m」的文件」。這裏是我的代碼,但我有問題,編譯時我嘗試打開該文件爲outfile.open(out);寫入文件名不確定的文件

int main(int argc, char *argv[]){ 
    readFile(argv); 
    int x=0; 
    compile(x); 
    mystring += "halt"; 
    cout << mystring<< endl; 
    string out = argv[1]; 
    out.resize(out.size()-1); 
    out += "m"; 
    ofstream outfile; 
    outfile.open(out); 
    outfile << mystring; 
    outfile.close(); 
    return 0; 
} 

任何人都知道可能是什麼問題?因爲它編譯時,我給這樣的參數:outfile.open("blah.m");感謝您的答覆。

+2

我建議你編輯代碼和第一添加聲明所有的變量。 – Blacktempel

+1

你的意思是說你有編譯錯誤?在這種情況下,您應該編輯您的問題以包含完整和未編輯的錯誤日誌。 –

回答

1

我猜你使用舊的編譯器沒有C++ 11,太行

outfile.open(out); 

編譯失敗,因爲NI C++ 98開放接受字符指針只是沒有的std ::字符串。該行更改爲

outfile.open(out.c_str()); 

,它應該編譯

+0

工作。謝謝!另外,你知道如何更新u ++ 13.10中的g ++到C++ 11嗎? –

+0

@AtakanArıkan添加標誌'-std = C++ 11'。 –

0

嘗試這種情況:

outfile.open(out.c_str()); 

在C++ 98,的open()參數是const char *

0

容易的解決方案,而是具體到本案例:

size_t pos = strlen(argv[1]) - 1; 
argv[1][pos] = 'm'; 

std::ofstream out(argv[1]); // Easier to just create the stream already opened.