c++中使用ifstream打开文件失败。
高洛峰
高洛峰 2017-04-17 14:24:04
[C++讨论组]
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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(2)
PHPz

具体的错误是怎样的哦?我自己写了个,然后试了下,可以打开,你复制下试试。会不会是忘了从文件读入,还是用的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;
}
伊谢尔伦

被打开的文件存在吗?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号