[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>读取
函数都在 <stdio.h>头文件中。
1.从文件中读取单个字节
函数:
int fgetc(FILE * fptr);
当调用该函数时,文件指针返回它指向的字符,接着文件指针前进到下个字符。该调用函数返回一个文件缓冲区读出字符的整数值。
例子:
int date; while((date=fgetc(fp)!=EOF)//其中fp为文件指针 { printf("%d\n",date); }
代码演示了如何调用fgetc() 函数。每次调用函数时, 检查返回值是否为E OF以查看是否已到文件末尾。所以,
在while循环中我们能迭代并每次读取一个字符。
2.从文件中读取字符串
用 fgets()函数:
char * fgets(char * str,int length,FILE * fptr);
该函数接受一个字符指针作为输人参数,该字符是由缓冲区中读取的字符填充的,第二个输人参数是需要读出的字符长度
。需要读取的字符长度始终为length-1。最后一个参数是调用fopen() 函数后得到的文件指针变量。
一旦成功打开,该函数会返回指向第一个输人参数的指针,打开失败会返回NULL。从输人字符串读取字符时, 该函数也在字符串末尾添加终止符‘\0’
char s[20]; fgets(s,15,fp); puts(s);
3.从文件读取数据块
函数:fread();
size_t fread( void * ptr,size_t size, size_t n, FILE * ptr);
第一个参数void*ptr为void类型指针输人参数,指向将被复制的缓冲区读取数据。 第二个参数*size_tsize指定每个数据块大小*的输入参数。
第三个参数size_tn*指定要读取的字节数*。 第四个参数是FILE*fptr。该函数**返回参数返回成功时读取的数据量**。当有读取错误或文件指针到E
OF时, 返回参数值和第三个输人参数值存在差异。 int n=5; int res; char * s; s=(char *)malloc(sizeof(
char)*n); res=(s,sizeof(char),n-1,fp);
* 格式化读取
函数:fscanf();
** int fsanf(FILE * stream, const char * format,–);
第一个参数是文件指针。 第二个参数指定格式。 如果格式匹配返回参数为一个整数,即数据量;如果格式不匹配返回0,成功时条件为真,失败时返回EOF。 int
index; char s1[20],s2[20]; fscanf(fp,"%d %s %s",&index,s,s1);