[英]Unexpected output while reading from file
我正在使用來自cplusplus.com的代碼,該代碼正在打開文件,從文件中讀取信息,打印並關閉。 因此,這是代碼:
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
int main () {
char str[256];
std::cout << "Enter the name of an existing text file: ";
std::cin.get (str,256); // get c-string
std::ifstream is(str); // open file
while (is.good()) // loop while extraction from file is possible
{
char c = is.get(); // get character from file
if (is.good())
std::cout << c;
}
is.close(); // close file
return 0;
}
因此,具有此代碼的文件稱為“ prog.cpp”,並保存在Documents / Test文件夾中。 在同一文件夾中,有一個名為“ hello.txt”的文本文件,該文件包含以下內容:
hello to everyone
因此,在我運行程序之后,預期結果是“大家好”,但是我有:
hsdhs131313dhhsd
因此,沒有其他文件。 錯誤在哪里? 我做錯了什么? 使用Mac OSX。 這是termianl窗口,sublime文本窗口和finder的屏幕截圖。 預先感謝您的幫助
當程序要求輸入文件名時,輸入完整路徑:
Enter the name of an existing text file: /Users/ratkke/Documents/Test/prog/hello.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.