程式碼示例:
#include
<stdio.h>
<stdlib.h>
<string.h>
typedef
struct
STU_INFO
{
char
stuNo[7];
stuName[11];
}
STU_INFO;
int
main(int
argc,
*argv[])
FILE
*in,
*out;
info;
*filePath
=
"c:/stu.dat";
if
((out
fopen(filePath,
"a+"))
==
NULL)
printf("open
file
error");
return
-1;
printf("請輸bai入du0結束\n");
while(1)
printf("請輸入學號(長度6)
exac
100000:\n");
scanf("%s",
stuNo);
(atol(stuNo)
0)
break;
else
&&
strlen(stuNo)
6)
printf("請輸入姓名(長度<=10)
張三:\n");
stuName);
fprintf(out,
"%s,%s\n",
stuNo,
printf("輸入錯誤\n");
continue;
fclose(out);
printf("\n
請輸入要查詢的zhi學號:dao\n");
((in
"r"))
while
(!feof(in))
fscanf(in,
"%6s,%s",
info.stuNo,
info.stuName);
atol(info.stuNo))
printf("學號:%s
姓名:%s\n",
(feof(in))
printf("不存在這個學生!!!\n");
fclose(in);
0;
程式碼示例:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
typedef
struct
STU_INFO
{
char
stuNo[7];
char
stuName[11];
}
STU_INFO;
int
main(int
argc,
char
*argv[])
{
FILE
*in,
*out;
char
stuNo[7];
char
stuName[11];
STU_INFO
info;
char
*filePath
=
"c:/stu.dat";
if
((out
=
fopen(filePath,
"a+"))
==
NULL)
{
printf("open
file
error");
return
-1;
}
printf("請輸bai入du0結束\n");
while(1)
{
printf("請輸入學號(長度6)
exac
100000:\n");
scanf("%s",
stuNo);
if
(atol(stuNo)
==
0)
{
break;
}
else
if
(atol(stuNo)
&&
strlen(stuNo)
==
6)
{
printf("請輸入姓名(長度<=10)
exac
張三:\n");
scanf("%s",
stuName);
fprintf(out,
"%s,%s\n",
stuNo,
stuName);
}
else
{
printf("輸入錯誤\n");
continue;
}
}
fclose(out);
printf("\n
請輸入要查詢的zhi學號:dao\n");
scanf("%s",
stuNo);
if
((in
=
fopen(filePath,
"r"))
==
NULL)
{
printf("open
file
error");
return
-1;
}
while
(!feof(in))
{
fscanf(in,
"%6s,%s",
info.stuNo,
info.stuName);
if
(atol(stuNo)
==
atol(info.stuNo))
{
printf("學號:%s
姓名:%s\n",
info.stuNo,
info.stuName);
break;
}
}
if
(feof(in))
{
printf("不存在這個學生!!!\n");
}
fclose(in);
return
0;
}