開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C
問題(Question):
書本範例: 利用學生ID搜尋,找出學生的資料
餵入的資料(Input):
#include<stdio.h>
struct mydata
{
int id;
char name[12];
int score;
}buffer;
int main(void)
{
FILE *fptr;
int idkey;
printf("Input ID to search");
scanf("%d",&idkey);
fptr=fopen("c:\\c_prog\\abc.txt","r");
if(fptr!=NULL)
{
while(!feof(fptr)&&(idkey!=buffer.id))
{
fread(&buffer,sizeof(buffer),1,fptr)
if(buffer.id==idkey)
{
printf("Yes! You got it!\n");
printf("Student's ID:%d\n",buffer.id);
printf("Student's NEME:%s\n",buffer.name);
printf("Student's score:%d\n",buffer.score);
}
}
}
...
...(以下省略
}
補充說明(Supplement):
程式打得有點多, 主要是想問為何 buffer.id 就可以找到資料
而不需要像 buffer[1].id 這樣呢?
所以是buffer只有一筆資料, 不需要buffer[i].id的意思另一個問題是fread(,,1,) 這個1是取1筆資料後會跳至下一筆資料嗎? 像是getc 那樣?