// (1)
main(){
	FILE *fp;		// (1)宣告檔案指標 file pointer
	char s[100];	// 文字變數 s, 可存99字符
	char t[100];
	int i=0;
	fp = fopen("school.txt","r");	// (2)開啟文字檔 school.txt
	if(fp==NULL) return -1;		// (3)開啟失敗 (#define NULL 0)

	while(!feof(fp)){			// (4)未到fp檔末 not end-of-file
		fgets( s, 100, fp );	// (5)每次從fp讀取一行文字(不多於100字符)
		s[ strlen(s)-1 ] = '\0';
		if(feof(fp)) break;		// (6)若已到檔末,便離開
		i++;
		strncpy(t,s+5,6); t[6]='\0';	// 抽取部分文字
		printf("%i %s %s\n",i,s,t);	// 在螢幕上顯示文字s
	}

	fclose(fp);		// (7)關閉文字檔
	return 0;


// (2)
main(){
	FILE *fp1, *fp2;		// (1)宣告檔案指標 file pointers
	char ch,filename1[]="file1.txt",filename2[]="file2.txt";

	fp1=fopen("file1.txt","r");	// (2)讀read
	fp2=fopen("file2.txt","w");	//    寫write
/*
或	if(!(fp1=fopen(filename1,"r"))) return 999;	// (3)開啟失敗
	if( (fp2=fopen(filename2,"w"))==NULL) return 911;
*/
	while( !feof(fp1) ){		// (4)未到fp1檔末 not end-of-file
		fscanf (fp1,"%c",&ch);	// (5)從fp1讀取一個字符
		fprintf(fp2,"%c", ch);	// (6)寫入fp2
		printf("%c",ch);	// 輸出ch在顯示器上
	}

	fclose(fp1);			// (7)關閉文字檔
	fclose(fp2);
	return 0;
}


// (3)
	rewind(fp);				// (1)返回檔案開頭
	while(!feof(fp)){			// (2)未到fp檔末 not end-of-file
		fgets( s, 100, fp );	// (3)每次從fp讀取一行文字(不多於100字符)
		printf("%i\n", ftell(fp));	// (4)顯示現時位置所在
	}

	// 從___開始,向前移動10個字符
	fseek(fp, 10, 0);	// 從檔案頭
	fseek(fp, 10, 1);	// 從現在所在位置
	fseek(fp, 10, 2);	// 從檔案尾