| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:如何读取计算机中的文件
只看楼主 加入收藏
ClarenceC
Rank: 2
等 级:论坛游民
帖 子:54
专家分:18
注 册:2014-1-10
结帖率:87.5%
收藏
 问题点数:0 回复次数:1 
如何读取计算机中的文件
int reservefile(string &filename,vector<string>& cont)
{
    ifstream infile;
    infile.close();
    infile.clear();
    infile.open(filename.c_str());
    if(!infile)
        return 1;
    string line;
    while(getline(infile,line)){
        cont.push_back(line);
    }
    infile.close();
    if(infile.eof())
        return 2;
    if(infile.bad())
        return 3;
    if(infile.fail())
        return 4;
}
int main()
{
    vector<string> cont;
    string filename;
    cout<<"enter your file name : "<<endl;
    cin>>filename;
        //检验错误
    switch(reservefile(filename,cont)){
    case 1:
        cout<<"error: can't open : "<<filename<<endl;
        return -1;
    case 3:
        cout<<"error: system failure "<<endl;
        return -1;
    case 4:
        cout<<"error: read failure "<<endl;
        return -1;
    }
    istringstream instr;
    string str;
         //输出每一个单词
    for(vector<string>::const_iterator ip=cont.begin();ip!=cont.end();++ip){
        instr.str(*ip);
        while(instr>>str){
            cout<<str;
        }
            instr.clear();
    }
    return 0;
}
现在希望读取计算机中的文件,如何确定文件的路径?请指教
                                                      谢谢
搜索更多相关主题的帖子: 计算机 return 如何 
2014-02-09 17:59
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
收藏
得分:0 
文件的路径?
ifstream的参数可以直接接受路径,不写路径为EXE当前目录
 "C:\\wqnmlgb\\wqnmlgb.txt"
2014-02-16 20:05
快速回复:如何读取计算机中的文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011300 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved