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

查看:3136
本文介绍了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;
}

这篇关于c++中使用ifstream打开文件失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆