标题:在c++primer有关文件流的问题
只看楼主
shfe
Rank: 1
等 级:新手上路
帖 子:17
专家分:5
注 册:2013-2-24
结帖率:66.67%
已结贴  问题点数:20 回复次数:1 
在c++primer有关文件流的问题
请帮我看一下下面的程序,我怎么运行不出来
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>

int main()
{
string ifile;
cout<<"Please enter file to sort:";
cin>>ifile;

//构造一个ifstream 输入文件对象
ifstream infile(ifile.c_str());

if(!infile)
{
cerr<<"error:unable to open input file:"<<ifile<<endl;
return -1;
}
string ofile=ifile+".sort";

//构造一个ofstream输出文件对象
ofstream outfile(ofile.c_str());
if(!outfile)
{
cerr<<"error:unable to open output file:"<<ofile<<endl;
return -2;
}

string buffer;
vector<string,allocator>text;

int cnt = 1;
while(infile>>buffer)
{
text.push_back(buffer);
cout<<buffer<<(cnt++%8?" ":"\n");
}
sort(text.begin(),text.end());

//把排序后的词打印到outfile
vector<string,allocator>::iterator iter=text.begin();
for(cnt =  1;iter!=text.end();++iter,++cnt)
outfile<<*iter<<(cnt%8?" ":"\n");
return 0;
}
搜索更多相关主题的帖子: vector include return 
2013-04-16 16:57
zhuxiaoneng
Rank: 4
等 级:业余侠客
威 望:2
帖 子:51
专家分:215
注 册:2013-4-10
得分:20 
修改如下:
程序代码:
#include "stdafx.h"

#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include <iostream>

using namespace std;

int main()
{
    string ifile;
    cout<<"Please enter file to sort:";
    cin>>ifile;

    //构造一个ifstream 输入文件对象
    ifstream infile(ifile.c_str());

    if(!infile)
    {
        cerr<<"error:unable to open input file:"<<ifile<<endl;
        return -1;
    }
    string ofile=ifile+".sort";

    //构造一个ofstream输出文件对象
    ofstream outfile(ofile.c_str());
    if(!outfile)
    {
        cerr<<"error:unable to open output file:"<<ofile<<endl;
        return -2;
    }

    string buffer;
    vector<string>text;

    int cnt = 1;
    while(infile>>buffer)
    {
        text.push_back(buffer);
        cout<<buffer<<(cnt++%8?" ":"\n");
    }
    sort(text.begin(),text.end());

    //把排序后的词打印到outfile
    vector<string>::iterator iter=text.begin();
    for(cnt =  1;iter!=text.end();++iter,++cnt)
        outfile<<*iter<<(cnt%8?" ":"\n");
    return 0;
}
2013-04-16 17:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-404424-1-1.html




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

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