Hello Feixy

原创文章,未经本人允许禁止转载。

//C方式, 调用的函数繁多
//fopen,fseek,ftell,fseek,malloc,fread,fclose,free.
void foo()
{
    FILE* fp=fopen(sFileName,"rb");
    fseek(fp,0,SEEK_END);
    int len = ftell(fp);
    fseek(fp,0,SEEK_SET);
    char* s = (char*)malloc(len);
    fread(s,1,len,fp);
    fclose(fp);
    fwrite(s,1,len,stdout);//output
    free(s);
}
//C++方式,易懂
void foo()
{
    ifstream fs(sFileName.c_str(),ios::binary);
    stringstream ss ;     
    ss << fs.rdbuf();
    fs.close();
    string str = ss.str();//read into string
}
//C++方式,高大上
//string的构造用了一个模版函数
void foo()
{
    std::ifstream ifs(sFileName.c_str());
    std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>(0));
    ifs.close();
}

原创文章,未经本人允许禁止转载。

posted on 2016-09-06 20:10  飞翔雨  阅读(1433)  评论(0编辑  收藏  举报