文件的读写操作

本文已ios 为例:

1)文件读取操作

FILE * fp=NULL;
-(int)readFile:(Byte *)mydate :(long int)myoff :(long int)mylength {
    NSString *sn = [NSString stringWithFormat:@"temp_audio"];
    NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),sn];
    const  char *path=[aPath UTF8String];

    if(fp==NULL)
    {
        fp = fopen(path, "r");
     }
    if(fp==NULL)
    {
        return 0;
    }
    fseek(fp,myoff,0);
   int ret= fread(mydate, 1, mylength, fp);

    return ret;
    //    fclose(fp);

2)文件写入操作

 

 FILE * fp=NULL;

-(void)savaFile:(Byte *)bdata :(long int)length{

    NSString *sn = [NSString stringWithFormat:@"temp_audio"];
    NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),sn];
    const  char *path=[aPath UTF8String];

    if(fp==NULL)
    {
    fp = fopen(path, "ab+");
    if(fp==NULL)
    {
        fp = fopen(path, "w+b");
    }
    }
    fseek(fp,0,SEEK_END);
    fwrite(bdata,1,length,fp);
//    fclose(fp);
}

 

你可能感兴趣的:(文件读取,文件写入)