[英]Why am i getting these invalid characters before my file data?
我試圖通過getline
函數或fileContents.assign( (istreambuf_iterator<char>(myFile)), (istreambuf_iterator<char>()));
無論哪種方式都給了我上面顯示的輸出圖像。
第一種方式:
string fileContents;
ifstream myFile("textFile.txt");
while(getline(myFile,fileContents))
cout<<fileContents<<endl;
替代方式:
string fileContents;
ifstream myFile(fileName.c_str());
if (myFile.is_open())
{
fileContents.assign( (istreambuf_iterator<char>(myFile) ),
(istreambuf_iterator<char>() ) );
cout<<fileContents;
}
該文件以這些字符開頭,很可能是一個BOM,用於告訴您文件的編碼是什么。
您可能無法在 Windows 記事本中看到它們,因為記事本隱藏了編碼字節。 獲得一個不錯的文本編輯器,它可以讓您查看文件的二進制文件,並且您將看到這些字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.