c++ 利用file文件流读取文件
来源:互联网 发布:python基础教程怎么样 编辑:程序博客网 时间:2024/06/02 18:49
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;
}
//第一先获取文件的路径
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;
}
- c++ 利用file文件流读取文件
- 利用PHP读取C语言Struct文件
- C读取文件流
- 多线程读取文件File
- java File读取文件
- File类 读取文件
- File 读取文件
- 利用File自动更新文件
- 关于流和File读取文件
- File文件读取与输入输出流
- File文件读取与输入输出流
- 利用流迭代器读取文件
- QT读取文件路径用C语言File打开
- 【C语言】FILE读取文件的'\o'和'\n'。
- file 读取写入文件 HTML
- java File文件读取大全
- File类3 文件读取
- input file 读取文件内容
- 实现类似百度文库的在线预览office文档
- 1.1.1---Greedy Gift Givers
- HDU 2084 数塔 DP入门
- 求余数的妙用
- 标签
- c++ 利用file文件流读取文件
- 单向hash函数
- 大整数相加
- Mac OS Terminal 基本指令(全)
- python-str函数详解
- 关于《JavaScript高级程序设计(第2版)》中的一句话
- Bootloader的执行过程
- 黑马程序员_C#代码快速注释。
- 黑马程序员---线程1