注册 登录
编程论坛 C++教室

大家快进来帮帮忙

绿茶盖儿 发布于 2011-11-07 23:41, 745 次点击
以下是一段C语言代码,就是读取BitComet.xml文件里内容,谁能帮我把这段代码改为纯C++代码?我目前只学了C,还没学C++,所以不会
程序代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char ch;   
    if((fp=fopen("BitComet.xml","r"))==NULL)
    {
         printf("can't open file.\n");
         exit(0);
    }
    ch = fgetc(fp);
    while(ch != EOF)
    {
        putchar(ch);
        ch = fgetc(fp);
    }
    fclose(fp);
    return 0;
}


[ 本帖最后由 绿茶盖儿 于 2011-11-7 23:48 编辑 ]
1 回复
#2
rjsp2011-11-08 08:27
程序代码:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    // 方法1
    {
        ifstream ifs("BitComet.xml");
        for( char buf[4096]; ifs.read(buf,sizeof(buf)/sizeof(buf[0])), ifs.gcount()!=0 && cout.write(buf,ifs.gcount()); );
    }
    // 方法2
    {
        copy( istreambuf_iterator<char>(ifstream("BitComet.xml")), istreambuf_iterator<char>(), ostreambuf_iterator<char>(cout) );
    }
    // 方法3
    {
        cout.operator<<( std::ifstream("BitComet.xml").rdbuf() );
    }

    return 0;
}
1