获取最后n行数据
把每个数组看成是1行
#include <stdio.h>
int main(void)
{
// 示例数据 成员大小最多20字节 成员数量最多5个
char s1[5][20] = {"a1", "a2", "a3", "a4", "a5"};
char s2[5][20] = {"b1", "b2", "b3", "b4", "b5"};
char s3[5][20] = {"c1", "c2", "c3", "c4", "c5"};
FILE *fp;
fp = fopen("output.bin", "wb");
fwrite(s1, 20, 5, fp);
fwrite(s2, 20, 5, fp);
fwrite(s3, 20, 5, fp);
fclose(fp);
#define N 10
char data[N][20];
fp = fopen("output.bin", "rb");
// 上面写入的数据 每个数组的大小是20*5=100
// 每个数组看成是1行 要获取最后n行,就是n*100,这里获取最后2行的数据
fseek(fp, -2 * 100L, SEEK_END);
size_t items_read = fread(data, 20, N, fp); // 保存实际读取的数量
fclose(fp);
// 打印读取到的数据
for (int i = 0; i < items_read; ++i)
{
printf("%s\n", data[i]);
}
return 0;
}
标签:fp,20,读取,二进制,C语言,char,数组,data From: https://www.cnblogs.com/yuyanc/p/17814770.html