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

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文件右键以管理员身份运行了,但还是打不开文件。
网上也没找到解决方法。请问要怎么办。谢谢。


被打开的文件存在吗?


具体的错误是怎样的哦?我自己写了个,然后试了下,可以打开,你复制下试试。会不会是忘了从文件读入,还是用的cin读哦?

#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;
}

我们试一下下面的这个。。。, 直接把字符读到文件,,接着就打开,,看看能不能成功打开呢?我试了可以, 你试试行不。。。。

#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;
}
【热门文章】
【热门文章】