c++ 利用file文件流读取文件

来源:互联网 发布:python基础教程怎么样 编辑:程序博客网 时间:2024/06/07 10:22
string getFileByName(string pFileName){  
    //第一先获取文件的路径  
    string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;  
    CCLOG("path = %s",path.c_str());  
    
    //创建一个文件指针  
    FILE* file = fopen(path.c_str(), "r");  
    
    if (file) {  
        char* buf;  //要获取的字符串  
        int len;    //获取的长度  
        /*获取长度*/  
        fseek(file, 0, SEEK_END);   //移到尾部  
        len = ftell(file);          //提取长度  
        rewind(file);               //回归原位  
        CCLOG("count the file content len = %d",len);  
        //分配buf空间  
        buf = (char*)malloc(sizeof(char) * len + 1);  
        if (!buf) {  
            CCLOG("malloc space is not enough.");  
            return NULL;  
        }  
        
        //读取文件  
        //读取进的buf,单位大小,长度,文件指针  
        int rLen = fread(buf, sizeof(char), len, file);  
        buf[rLen] = '\0';  
        CCLOG("has read Length = %d",rLen);  
        CCLOG("has read content = %s",buf);  
        
        string result = buf;  
        fclose(file);  
        free(buf);  
        return result;  
    }  
    else  
        CCLOG("open file error.");  
    
    return NULL;  
}  
原创粉丝点击