向文件中写数据的方法

来源:互联网 发布:西安少儿编程培训班 编辑:程序博客网 时间:2024/04/30 01:15

static inline int raw_write(const char *filename, const void *buffer, size_t bytes) {
        FILE *f = fopen(filename, "ab+"); // this will create file automatically
        if (f != NULL) {
            int ret = fwrite(buffer, 1, bytes, f);
            ret |= fclose(f);
            return ret;
        }
        return -1;
}

#ifdef HAVE_XXXX_AUDIO
#ifdef XXXX_AUDIO_LOGGER
#define MAX_FILE_LENGTH (20000000)
static inline int check_path(const char * path)
{
    char tmp[PATH_MAX];
    int i = 0;

    while(*path)
    {
        tmp[i] = *path;

        if(*path == '/' && i)
        {
            tmp[i] = '\0';
            if(access(tmp, F_OK) != 0)
            {
                if(mkdir(tmp, 0770) == -1)
                {
                    ALOGE("mkdir error! %s",(char*)strerror(errno));
                    return -1;
                }
            }
            tmp[i] = '/';
        }
        i++;
        path++;
    }
    return 0;
}

static inline int dump_recpcm(const char * filepath, const void * buffer, size_t bytes, const char * property)
{
    char value[PROPERTY_VALUE_MAX];
    int ret;
    property_get(property, value, "0");
    int bflag=atoi(value);
    if(bflag)
    {
        ret = check_path(filepath);
        if(ret<0)
        {
            ALOGE("dump fail!!!");
   return -1;
        }
        else
        {
            FILE * fp= fopen(filepath, "ab+");
            if(fp!=NULL)
            {
                long int position =0;
                position = ftell (fp);
                if(position> MAX_FILE_LENGTH)
                {
                    rewind(fp);
                }
                fwrite(buffer,1,count,fp);
                fclose(fp);
    return 1;
            }
            else
            {
                ALOGE("dump %s fail",property);
    return -1;
            }
        }
    }
 else
 {
  ALOGE("bflag %d is fail",bflag);
  return -1;
 }
}
#endif
#endif

 

原创粉丝点击