c++中使用ifstream打开文件失败。
高洛峰
高洛峰 2017-04-17 14:24:04
0
2
431
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文件右键以管理员身份运行了,但还是打不开文件。
网上也没找到解决方法。请问要怎么办。谢谢。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
PHPzhong

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;
}
伊谢尔伦

Does the file being opened exist?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template