實例四:隨機讀寫。在C:\\TEST.TXT 文件中創建如下字符串:我愛你,中國
使用隨機讀寫,輸出"中國"兩個字,以爲中文字符佔兩個字節,所以要從文件首部向後偏移7個字節,逗號是英文字符,佔用一個字節。
#include<stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp ;
char c;
if((fp=fopen("c:\\test.txt","rb"))!=NULL)
{
rewind(fp);
fseek(fp,7L,0);//偏移7個字節
c=fgetc(fp);
while(c!=EOF)
{
printf("%c",c);
c=fgetc(fp);
}
printf("\n");
}
else
{
printf("file not exist!");
exit(1);
}
return 0;
}