search

Home  >  Q&A  >  body text

c++中使用ifstream打开文件失败。

ifstream input("D:\\trans.txt");
ifstream input;
inout.open("D:\\trans.txt",ifstream::in);
wchar_t const name[] = L"D:\\trans.txt";
ifstream input(name);

上面的几种方法都试了,还在exe文件右键以管理员身份运行了,但还是打不开文件。
网上也没找到解决方法。请问要怎么办。谢谢。

高洛峰高洛峰2773 days ago426

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 14:26:04

    What is the specific error? I wrote it myself and tried it. It can be opened. You can copy it and try it. Could it be that I forgot to read from the file, or did I use cin to read?

    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream in("D:\zz.txt");
        string res;
        while (getline(in, res))
        {
            cout << res << endl;
        }
        return 0;
    }

    Let’s try this one below. . . , directly read the characters into the file, and then open it, and see if it can be opened successfully? I can try it, can you try it? . . .

    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         ofstream ou("D:\zz.txt");
         string test("hello world");
         ou << test << endl;
        ifstream in("D:\zz.txt");
        if (in.is_open())
        {
            cout << "opened!\n";
        }
        string res;
        while (getline(in, res))
        {
            cout << res << endl;
        }
        return 0;
    }
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:26:04

    Does the file being opened exist?

    reply
    0
  • Cancelreply