我做了一個類叫做FileReader。這是我閱讀這門課的功能。它打開一個文件並讀取它。當然,它將文件的內容放入我的課程的一個名爲「內容」的變量中。它在最後一行。字符串有一個' n'太多
std::string file_content;
std::string temp;
std::ifstream file;
file.open(filepath,std::ios_base::in);
while(!file.eof()){
temp.clear();
getline(file, temp);
file_content += temp;
file_content += '\n';
}
file_content = file_content.substr(0, file_content.length()-1); //Removes the last new line
file.close();
content = file_content;
的文件,我打開具有以下內容:
「你好\ nWhat氏達\ nCool」。
當然,我並沒有在我的文本文件中精確地編寫\ n。但正如你所看到的,最後沒有新的路線。
我的問題是,「內容」,每當我打印到屏幕上,在最後一個新行。但我刪除了最後一個新行......出了什麼問題?
你是行結尾\ r \ n還是\ n? – bizzehdee
如果你使用'Windows',行分隔符總是'\ r \ n',而Unix只使用'\ n' ... –
我正在使用Linux –