運行環境:ubuntu 64bit
實現方法:使用fscanf偏移文件位置,再對文件進行讀取。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
/* code */
FILE *fin;
char buf[1024];
int x,y;
int i;
fin = fopen("/home/mycode/abc.txt","r");
for(i = 0;i < 2;i++)
{
fscanf(fin,"%*[^\n]%*c");
/******************************************************
%* : 是“跳過”
[^\n] : 字符串的分隔符是"\n", 中括號裏可以寫 分隔符 表
%*[^\n] : 跳過 \n 前的所有字符串。
%*c : 是“跳過”行尾 的 換行符
******************************************************/
}
//while((fgets(buf,1024,fin)) != NULL)
#if 0
//這裏獲取第3行到文件尾的數據
while(fgets(buf,1024,fin))
{
printf("buf : %s\n", buf);
memset(buf,0,1024);
}
#endif
//這裏只是獲取第3行數據
fgets(buf,1024,fin);
printf("buf : %s\n",buf );
fclose(fin);
return 0;
}
結果如下: