fseek(),fread(),fwrie(),memset()

函数名:fseek函数

头文件:#include<stdio.h>

功能:把与fp有关的文件位置指针放到一个指定位置。

格式:  int fseek(FILE *stream, long offset, int fromwhere);

范例一:fseek(fp, 0L, SEEK_END);

解释:文件指针定位到文件末尾,偏移0个字节

范例二:  fseek(fp,50L,0);或fseek(fp,50L,SEEK_SET);

解释:其作用是将位置指针移到离文件头50个字节处。

起始点对应的数字代表的文件位置
SEEK_SET0文件开头
SEEK_CUR1文件当前位置
SEEK_END2文件末尾

说明:

       offset:偏移量
       fromwhere:起始位置

      其中,“位移量”是long型数据,它表示位置指针相对于“起始点”移动的字节数。

      如果位移量是一个正数,表示从“起始点”开始往文件尾方向移动;

      如果位移量是一个负数,则表示从“起始点”开始往文件头方向移动。

     “起始点”不能任意设定,它只能是在stdio.h中定义的三个符号常量之一:

注意:  

      fseek函数的文件指针,应该为已经打开的文件。如果没有打开的文件,那么将会出现错误。

 

fread()定义于头文件<stdio.h>

1

2

3

4

5

size_t freadvoid *buffer, size_t size, size_t count, FILE *stream );//C99前

 

 

 

size_t freadvoid *restrict buffer, size_t size, size_t count, FILE *restrict stream );//C99起

从给定输入流stream读取最多count个对象到数组buffer中(相当于以对每个对象调用size次fgetc),把buffer当作unsigned char数组并顺序保存结果。流的文件位置指示器前进读取的字节数。

若出现错误,则流的文件位置指示器的位置不确定。若没有完整地读入最后一个元素,则其值不确定。

参数

buffer指向要读取的数组中首个对象的指针
size每个对象的大小(单位是字节)
count要读取的对象个数
stream输入流

返回值

返回成功读取的对象个数,若出现错误或到达文件末尾,则可能小于count。

若size或count为零,则fread返回零且不进行其他动作。

fread不区分文件尾和错误,因此调用者必须用feofferror才能判断发生了什么。

 

函数原型

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

参数

  • ptr-- 这是指向要被写入的元素数组的指针。

  • size-- 这是要被写入的每个元素的大小,以字节为单位。

  • nmemb-- 这是元素的个数,每个元素的大小为 size 字节。

  • stream-- 这是指向 FILE 对象的指针,该 FILE 对象指定了一个输出流。

功能

ptr所指向的数组中的数据写入到给定流stream中。

void *memset(void *s, int ch, size_t n);

函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。

memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体数组进行清零操作的一种最快方法 [1]  。

memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组,c:是赋给buffer的值,count:是buffer的长度.

https://www.cnblogs.com/xingyunblog/p/3675568.html

https://baike.baidu.com/item/fread/10942353?fr=aladdin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
int GetStrInBuf( char *profile, char *KeyName, char *KeyVal, int type) { char buf[512] = {0}; FILE *fp; char docat[128] = {0}; long profilelen; sprintf(docat, "nohup cat %s -> %s 0</dev/null", profile, TEMPTXT); system__popen(docat, NULL, 0); //printf("%s\n",docat); if( (fp=fopen( TEMPTXT,"r" ))==NULL ){ printf( "openfile [%s] error [%s]\n", profile, strerror(errno) ); return -1; } fseek(fp, 0, SEEK_END); profilelen = ftell(fp); fseek( fp, 0, SEEK_SET); fread(buf, 1, profilelen, fp); fclose(fp); char *json_str = (char *)malloc(strlen(buf)); strcpy(json_str, buf); struct json_object* json_obj = json_tokener_parse(json_str); // printf("Age: %d\n", json_object_get_int(json_object_object_get(json_obj, "age"))); memset(buf, 0x00, sizeof(buf)); if(type == 0) { strcpy(buf, json_object_get_string(json_object_object_get(json_obj, KeyName))); if(strlen(buf) == 0) { memset(KeyVal, 0x00, sizeof(KeyVal)); } else{ snprintf(KeyVal, (strlen(buf)+1), "%s", buf); printf("name:%s,valye:%s\n", KeyName, KeyVal); } } else { int str_value = json_object_get_int(json_object_object_get(json_obj, KeyName)); sprintf(KeyVal,"%d", str_value); printf("name:%s,valye:%s\n", KeyName, KeyVal); } memset(json_str, 0x00, sizeof(json_str)); free(json_str); json_object_put(json_obj); return 0; } int test(){ char buf_d[64]; memset(buf_d, 0x00, sizeof(buf_d)); GetStrInBuf(VTMP_JSON_CELL_INFO, "ipv4", buf_d, 0); char buf_a[64] = ""; memset(buf_a, 0x00, sizeof(buf_a)); GetStrInBuf(VTMP_JSON_CELL_INFO, "ipv6", buf_a, 0); if(strlen(buf_a) > 0) { char buf_c[128] = ""; memset(buf_c, 0x00, sizeof(buf_c)); sprintf(buf_c, "%s,%s",buf_d, buf_a); set_config_value(DATE_FILE,"gw5GIp",buf_c); } else{ set_config_value(DATE_FILE,"gw5GIp",buf_d); } printf("aa\n"); return 0; }
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值