iphone开发 C语言读写文件
来源:互联网 发布:黑莓priv淘宝靠谱么 编辑:程序博客网 时间:2024/05/18 00:50
关键是路径,其他文件操作和windows下一样
//这个函数得到存取的路径,读写文件获取路径比较保持一致,比如都用下面函数。
- (char*)GetFilePathByfileName:(char*)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strName = [NSString stringWithFormat:@"%s",filename];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:strName];
int len = [writablePath length];
char *filepath = (char*)malloc(sizeof(char) * (len + 1));
[writablePath getCString:filepath maxLength:len + 1 encoding:[NSString defaultCStringEncoding]];
return filepath;
}
//保存文件
- (BOOL)SaveFile:(char*)filename data:(char*)data len:(int)len
{
char *path = [self GetFilePathByfileName:filename];
FILE *fp = fopen(path,"wb");
if (fp==NULL)
{
return false;
}
int wlen = fwrite(data, 1, len, fp);
if (wlen!=len)
{
fclose(fp);
free(path);
return false;
}
fclose(fp);
free(path);
return true;
}
//读文件
- (BOOL)ReadFile:(char*)filename data:(char*)data len:(int*)len{
char *filepath = [self GetFilePathByfileName:filename];
FILE *fp = fopen(filepath,"rb");
if (fp == NULL)
{
free(filepath);
return false;
}
fseek(fp, 0, SEEK_END);
int filelen = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (*len < filelen)
{
fclose(fp);
free(filepath);
return false;
}
int rlen = fread(data, 1, filelen, fp);
if (rlen < filelen)
{
fclose(fp);
free(filepath);
return false;
}
*len = filelen;
fclose(fp);
free(filepath);
return true;
}
- iphone开发 C语言读写文件
- iphone开发 文件的读写
- iphone开发文件的读写
- c语言读写文件
- C语言读写文件
- C语言读写文件
- C语言文件读写
- C语言读写文件
- C语言文件读写
- C语言文件读写
- C语言读写文件
- C语言文件读写
- C语言读写文件
- C语言读写文件
- C语言读写文件
- C语言文件读写
- C语言读写文件
- C语言读写文件
- Jquery css函数实现动态操纵DOM节点的样式
- dns域名解析过程分析
- esponse.setContentType与 request.setCharacterEncoding 区别
- android---- Serializable 和 Parcelable 区别
- [原译]C#制作进度窗体
- iphone开发 C语言读写文件
- SQL Server CONVERT() 函数
- Java虚拟机结构与内存溢出 .
- CloudStack创建主存储失败(SR已经被使用),删除SR后成功
- I、P、B帧
- 爱情如江南的青花瓷
- shell测试
- 做为中国人,以下“历史片断”绝对颠覆你的想像!
- Study JQuery《4、事件和动画》