簡體   English   中英

我無法從文件中讀取

[英]I can't read from a file

我寫了一個代碼來制作“text1.txt”文件。 它工作正常,然后我一直試圖從文件中讀取,但每次is_open() function 不返回 true。 盡管如此,我還是按照它們在不同編譯器中的完全相同的方式復制了其他代碼,但它從來沒有用過。 我將如何解決這個問題:(

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    ifstream file1("text1.txt");
    string str;
    if(file1.is_open()){
        while(getline( file1, str)){
        cout<<str;
        }
    }
    else
        cout<<"the file is not open"<<endl;
    return 0;
}

你是如何運行你的程序的?

我見過的最常見的原因是您在 IDE(如 Visual Studio)中運行程序,而您的當前目錄不在您認為的位置。

嘗試輸入文件的完整路徑,看看您的問題是否消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM