[{"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}]
获取文件总行数:
int get_line(int fd) { int line = 0,prev,ret; char ch; prev =
lseek(fd,0,SEEK_CUR); //save last seek lseek(fd,0,SEEK_SET); while(1) { ret =
read(fd, &ch,1); if(ret <= 0) { lseek(fd,prev,SEEK_SET); return line; } if(ch
== '\n') line++; } }
文件位置指针偏移到某行:
int move_line(int fd,int line) { char ch; int n = 1,ret; while(1) { if(n ==
line) return 0; ret = read(fd,&ch,1); if(ret <= 0) return -1; if(ch == '\n') {
n++; } } }
完整代码:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/types.h> int get_line(int fd) { int line =
0,prev,ret; char ch; prev = lseek(fd,0,SEEK_CUR); //save last seek
lseek(fd,0,SEEK_SET); while(1) { ret = read(fd, &ch,1); if(ret <= 0) {
lseek(fd,prev,SEEK_SET); return line; } if(ch == '\n') line++; } } int
move_line(int fd,int line) { char ch; int n = 1,ret; while(1) { if(n == line)
return 0; ret = read(fd,&ch,1); if(ret <= 0) return -1; if(ch == '\n') { n++; }
} } int main(int argc, char **argv) { int fd_r,fd_w; int ret,size,del_line;
char ch; if(argc < 2) { fprintf(stderr,"Usage:%s <filename>\n",argv[0]);
exit(1); } fd_r = open(argv[1],O_RDONLY); if(fd_r < 0) { perror("open()");
exit(1); } fd_w = open(argv[1],O_RDWR,0666); if(fd_w < 0) { close(fd_r);
perror("open()"); exit(1); } printf("which line you wanna del:");
scanf("%d",&del_line); move_line(fd_w,del_line); move_line(fd_r,del_line + 1);
while(1) { ret = read(fd_r,&ch,1); if(ret <= 0) break; write(fd_w,&ch,1); }
if(ret < 0) { perror("read"); exit(1); } size = lseek(fd_w,0,SEEK_CUR);
ftruncate(fd_w,size); close(fd_r); close(fd_w); return 0; }