1、返回0通常是讀到了文件尾。如果確認(rèn)沒(méi)有到文件尾,可以用ferror檢查錯(cuò)誤。

創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供滄縣網(wǎng)站建設(shè)、滄縣做網(wǎng)站、滄縣網(wǎng)站設(shè)計(jì)、滄縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、滄縣企業(yè)網(wǎng)站模板建站服務(wù),10余年滄縣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
if ( ferror ( fp ) ){
printf("File read error.");
}
2、C語(yǔ)言中:fread是一個(gè)函數(shù)。從一個(gè)文件流中讀數(shù)據(jù),最多讀取count個(gè)元素,每個(gè)元素size字節(jié),如果調(diào)用成功返回實(shí)際讀取到的元素個(gè)數(shù),如果不成功或讀到文件末尾返回 0。
函數(shù)原型:size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;
參數(shù):
buffer 用于接收數(shù)據(jù)的內(nèi)存地址
size 要讀的每個(gè)數(shù)據(jù)項(xiàng)的字節(jié)數(shù),單位是字節(jié)
count 要讀數(shù)據(jù)項(xiàng)的個(gè)數(shù),每個(gè)數(shù)據(jù)項(xiàng)size個(gè)字節(jié).
stream 輸入流的指針
返回值:
返回實(shí)際讀取的元素個(gè)數(shù)。如果返回值與count不相同,則可能文件結(jié)尾或發(fā)生錯(cuò)誤。從ferror和feof獲取錯(cuò)誤信息或檢測(cè)是否到達(dá)文件結(jié)尾。
實(shí)例:
#includestdio.h
#includestring.h
int?main(void)
{
FILE*stream;
char?msg[]="this?is?a?test";
char?buf[20];
if((stream=fopen("DUMMY.FIL","w+"))==NULL)
{
fprintf(stderr,"Cannot?open?outputfile.\n");
return?0;
}
fwrite(msg,1,strlen(msg)+1,stream);
fseek(stream,0,SEEK_SET);
fread(buf,strlen(msg)+1,1,stream);
printf("%s\n",buf);
fclose(stream);
return0;
}
s-data=y;
寫(xiě)反了。
void insert_list(list *head,int i,int y)
{
list *p,*s;int j=0;
p=head;
do
{
p=p-next;
j++;
}while(ji-1);
if(j==i-1)
{
s=(list *)malloc(sizeof(list));
/* 這里寫(xiě)反了 y=s-data; */
s-data=y;
s-next=p-next;
p-next=s;
}
}
很久以前做的,看看能不能用?
//學(xué)生成績(jī)管理系統(tǒng)
//用戶名:admin 密碼:admin
#includestdio.h
#includeconio.h /*清屏*/
#include stdlib.h /*顯示目錄*/
#includestring.h
#define MAX 1000
void menu();
void input(); /*輸入數(shù)據(jù)函數(shù)*/
void sort(); /*排序數(shù)據(jù)函數(shù)*/
void display(); /*顯示數(shù)據(jù)函數(shù)*/
void display1(); /*顯示各科成績(jī)函數(shù)*/
void insert(); /*插入數(shù)據(jù)函數(shù)*/
void del(); /*刪除數(shù)據(jù)函數(shù)*/
void average(); /*平均值函數(shù)*/
void find(); /*查找數(shù)據(jù)函數(shù)*/
void save(); /*保存數(shù)據(jù)函數(shù)*/
void read(); /*讀出數(shù)據(jù)函數(shù)*/
void del_file(); /*刪除文件函數(shù)*/
void modify(); /*修改文件函數(shù)*/
int now_no=0;
struct student //定義學(xué)生信息
{
int
no;
// 學(xué)號(hào)
char name[20]; //姓名
char sex[4];
//性別
float score1; //成績(jī)1
float score2; //成績(jī)2
float score3; //成績(jī)3
float sort; //排序成績(jī)
float ave; //平均分
float sum; //總分
};
void main()
{
int h,flag1,flag2;
char name[20]="admin",password[10]="admin";
char person[20],password1[10];
printf("\t\t ******** 歡迎進(jìn)入學(xué)生成績(jī)管理系統(tǒng)!********\n\n");
printf("\t\t\t 用戶登錄\n\n");
for(h=0;h!=5;)
{
printf("\t\t\t\t 用戶名:");
gets(person);
flag1=strcmp(person,name);
printf("\t\t\t\t 密碼:");
gets(password1);
flag2=strcmp(password,password1);
if(flag1==0 flag2==0)
{
printf("\t\t\t\t 登陸成功!\n\n");
menu();
break;
}
else
{
printf ("\t\t\t 用戶名或密碼錯(cuò)誤!\n\n");
printf("\t\t\t 請(qǐng)注意:您還剩%d次機(jī)會(huì)!\n\n",4-h);
h++;
}
}
if(h=5)
printf ("對(duì)不起,您輸入的用戶名或密碼有誤,已被強(qiáng)制退出。\n");
}
struct student stu[MAX],*p;
void menu() /*主函數(shù)*/
{
int as;
char ch;
do{
printf("\n\n\n\n\t\t****************************************");
start: printf("\n\n\n\n\t\t\t
歡迎使用學(xué)生成績(jī)管理系統(tǒng)\n");
printf("\n\n\n\n\t\t******************按任意鍵繼續(xù)********************");
ch=getch();
}
while(!ch);
system("cls");
/*以下為功能選擇模塊*/
do
{
printf("\n\t\t\t\t1.錄入學(xué)生信息\n\t\t\t\t2.顯示學(xué)生總成績(jī)信息\n\t\t\t\t3.對(duì)總成績(jī)排序\n\t\t\t\t4.顯示學(xué)生單科成績(jī)排序\n\t\t\t\t5.添加學(xué)生信息\n\t\t\t\t6.刪除學(xué)生信息\n\t\t\t\t7.修改學(xué)生信息\n\t\t\t\t8.查詢(xún)學(xué)生信息\n\t\t\t\t9.從文件讀入學(xué)生信息\n\t\t\t\t10.刪除文件中學(xué)生信息\n\t\t\t\t11.保存學(xué)生信息\n\t\t\t\t12.退出\n");
printf("\t\t\t\t選擇功能選項(xiàng)(輸入所選功能前的數(shù)字):");
fflush(stdin);
/*可用可不用,用于清除緩存防止下次用scanf輸入時(shí)出現(xiàn)錯(cuò)誤*/
scanf("%d",as);
switch(as)
{
case 1:system("cls");
input();
break;
case 2:system("cls");
display();
break;
case 3:system("cls");
sort();
break;
case 4:system("cls");
display1();
break;
case 5:system("cls");
insert();
break;
case 6:system("cls");
del();
break;
case 7:system("cls");
modify();
break;
case 8:system("cls");
find();
break;
case 9:system("cls");
read();
break;
case 10:system("cls");
del_file();
break;
case 11:system("cls");
save();
break;
case 12:system("exit");
exit(0);
default:system("cls");
goto start;
}
}while(1); /*while(1),1表示真,所以while(1)表示永遠(yuǎn)循環(huán)下去*/
/*至此功能選擇模塊結(jié)束*/
}
void input() /*學(xué)生信息錄入模塊*/
{
inti=0;
char ch;
do
{
printf("\t\t\t\t1.錄入學(xué)生信息\n輸入第%d個(gè)學(xué)生的信息\n",i+1);
printf("\n輸入8位的學(xué)生學(xué)號(hào):");
scanf("%u",stu[i].no);
fflush(stdin);
printf("\n輸入學(xué)生姓名:");
fflush(stdin);
gets(stu[i].name);
printf("\n輸入學(xué)生性別:");
fflush(stdin);
gets(stu[i].sex);
printf("\n輸入學(xué)生成績(jī)1:");
scanf("%f",stu[i].score1);
printf("\n輸入學(xué)生成績(jī)2:");
fflush(stdin);
scanf("%f",stu[i].score2);
printf("\n輸入學(xué)生成績(jī)3:");
fflush(stdin);
scanf("%f",stu[i].score3);
printf("\n\n");
i++;
now_no=i;
printf("是否繼續(xù)輸入?(Y/N)");
fflush(stdin);
ch=getch();
system("cls");
}
while(ch!='n'ch!='N');
system("cls");
}
void sort() /*排序數(shù)據(jù)函數(shù)*/
{
struct student temp;
int i,j;
average();
for(i=1;inow_no;i++)
{
for(j=1;j=now_no-i;j++)
{
if(stu[j-1].avestu[j].ave)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
printf("排序已完成進(jìn)入功能2可進(jìn)行顯示\n");
system("pause");
system("cls");
}
void sort1() /*排序數(shù)據(jù)函數(shù)*/
{
struct student temp;
inti,j;
for(i=1;inow_no;i++)
{
for(j=1;j=now_no-i;j++)
{
if(stu[j-1].score1stu[j].score1)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
}
void sort2() /*排序數(shù)據(jù)函數(shù)*/
{
struct student temp;
int i,j;
for(i=1;inow_no;i++)
{
for(j=1;j=now_no-i;j++)
{
if(stu[j-1].score2stu[j].score2)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
}
void sort3() /*排序數(shù)據(jù)函數(shù)*/
{
struct student temp;
int i,j;
for(i=1;inow_no;i++)
{
for(j=1;j=now_no-i;j++)
{
if(stu[j-1].score3stu[j].score3)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
}
void display() /*顯示數(shù)據(jù)函數(shù)*/
{
inti;
char as;
average();
do
{
printf("\t\t\t班級(jí)學(xué)生信息列表\n");
printf("\t學(xué)號(hào)\t姓名\t性別\t成績(jī)1\t成績(jī)2\t成績(jī)3\t平均值\n");
for(i=0;inow_nostu[i].name[0];i++)
printf("\t%u%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
printf("\t\t\t按任意鍵返回主菜單");
fflush(stdin);
as=getch();
}
while(!as);
system("cls");
}
void display1() /*顯示數(shù)據(jù)函數(shù)*/
{
inti;
char as;
do
{
printf("\t\t\t班級(jí)學(xué)生score1成績(jī)排序\n");
printf("\t學(xué)號(hào)\t姓名\t性別\t成績(jī)1\n");
sort1();
for(i=0;inow_nostu[i].name[0];i++)
printf("\t%u%s\t%s\t%.2f\t\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1);
printf("\t\t\t班級(jí)學(xué)生score2成績(jī)排序\n");
printf("\t學(xué)號(hào)\t姓名\t性別\t成績(jī)2\n");
sort2();
for(i=0;inow_nostu[i].name[0];i++)
printf("\t%u%s\t%s\t%.2f\t\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score2);
printf("\t\t\t班級(jí)學(xué)生score3成績(jī)排序\n");
printf("\t學(xué)號(hào)\t姓名\t性別\t成績(jī)3\n");
sort3();
for(i=0;inow_nostu[i].name[0];i++)
printf("\t%u%s\t%s\t%.2f\t\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score3);
printf("\t\t\t按任意鍵返回主菜單");
fflush(stdin);
as=getch();
}
while(!as);
system("cls");
}
void insert() /*插入數(shù)據(jù)函數(shù)*/
{
char ch;
do
{
printf("\n\t\t輸入新插入學(xué)生信息\n");
printf("\n輸入學(xué)生學(xué)號(hào):");
scanf("%u",stu[now_no].no);
fflush(stdin);
printf("\n輸入學(xué)生姓名:");
fflush(stdin);
gets(stu[now_no].name);
printf("\n輸入學(xué)生性別:");
fflush(stdin);
gets(stu[now_no].sex);
printf("\n輸入學(xué)生成績(jī)1:");
fflush(stdin);
scanf("%f",stu[now_no].score1);
printf("\n輸入學(xué)生成績(jī)2:");
fflush(stdin);
scanf("%f",stu[now_no].score2);
printf("\n輸入學(xué)生成績(jī)3:");
fflush(stdin);
scanf("%f",stu[now_no].score3);
printf("\n\n");
now_no=now_no+1;
sort();
printf("是否繼續(xù)輸入?(Y/N)");
fflush(stdin);
ch=getch();
system("cls");
}
while(ch!='n'ch!='N');
}
void del() /*刪除數(shù)據(jù)函數(shù)*/
{unsigned long inum;
int i;
printf("輸入要?jiǎng)h除學(xué)生的學(xué)號(hào):");
fflush(stdin);
scanf("%u",inum);
for(i=0;inow_no;i++)
{
if(stu[i].no==inum)
{
if(i==now_no)now_no-=1;
else
{
stu[i]=stu[now_no-1];
now_no-=1;
}
sort();
break;
}
}
system("cls");
}
void save() /*保存數(shù)據(jù)函數(shù)*/
{
FILE *fp;
int i;
char filepath[20];
printf("輸入要保存的文件路徑:");
fflush(stdin);
gets(filepath);
if((fp=fopen(filepath,"w"))==NULL)
{
printf("\n保存失敗!");
exit(0);
}
for(i=0;inow_no;i++)
{
stu[i].sum=stu[i].score1+stu[i].score2+stu[i].score3;
stu[i].ave=stu[i].sum/3;
fprintf(fp,"\t%u%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
}
fclose(fp);
printf("學(xué)生信息已保存在%s中!\n",filepath);
system("pause");
system("cls");
}
void find() /*查詢(xún)函數(shù)*/
{
int i;
charstr[20],as;
do
{
printf("輸入要查詢(xún)的學(xué)生姓名:");
fflush(stdin);
gets(str);
for(i=0;inow_no;i++)
if(!strcmp(stu[i].name,str))
{
printf("\t學(xué)號(hào)\t\t姓名\t性別\t成績(jī)1\t成績(jī)2\t成績(jī)3\t平均值\n");
printf("\t%u\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
}
printf("\t\t\t按任意鍵返回主菜單");
fflush(stdin);
as=getch();
}
while(!as);
system("cls");
}
void average() /*求平均數(shù)*/
{
inti;
for(i=0;inow_no;i++)
{
stu[i].sum=stu[i].score1+stu[i].score2+stu[i].score3;
stu[i].ave=stu[i].sum/3;
}
}
void modify() /*修改數(shù)據(jù)函數(shù)*/
{
inti;
char str[20];
printf("輸入要修改的學(xué)生姓名:");
fflush(stdin);
gets(str);
for(i=0;inow_no;i++)
{
if(!strcmp(stu[i].name,str))
{
system("cls");
printf("\n\t\t輸入修改后學(xué)生信息\n");
printf("\n輸入學(xué)生學(xué)號(hào):");
fflush(stdin);
scanf("%u",stu[i].no);
printf("\n輸入學(xué)生性別:");
fflush(stdin);
gets(stu[i].sex);
printf("\n輸入學(xué)生成績(jī)1:");
fflush(stdin);
scanf("%f",stu[i].score1);
printf("\n輸入學(xué)生成績(jī)2:");
fflush(stdin);
scanf("%f",stu[i].score2);
printf("\n輸入學(xué)生成績(jī)3:");
fflush(stdin);
scanf("%f",stu[i].score3);
printf("\n\n");
sort();
break;
}
}
system("cls");
}
void read()
{
FILE *fp;
inti;
char filepath[20];
printf("輸入要讀入的文件路徑:");
fflush(stdin);
gets(filepath);
if((fp=fopen(filepath,"r"))==NULL)
{
printf("找不到%s文件!\n",filepath);
system("pause");
exit(0);
}
now_no=0;
for(i=0;iMAX!feof(fp);i++)
{
fscanf(fp,"\t%d\t%s\t%s\t%f\t%f\t%f\t%f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
now_no++;
}
fclose(fp);
printf("保存的在文件%s中的所有信息已經(jīng)讀入!\n",filepath);
system("pause"); /*按任意鍵繼續(xù)*/
system("cls");
}
void del_file()
{
FILE*fp;
charfilepath[20];
printf("輸入要?jiǎng)h除的文件路徑:");
fflush(stdin);
gets(filepath);
fp=fopen(filepath,"w");
fclose(fp);
printf("保存的在文件%s中的所有信息已經(jīng)刪除!\n",filepath);
system("pause");
system("cls");
}
新聞標(biāo)題:加載數(shù)據(jù)函數(shù)C語(yǔ)言 加載數(shù)據(jù)函數(shù)c語(yǔ)言怎么寫(xiě)
網(wǎng)站鏈接:http://chinadenli.net/article30/doghppo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站、、網(wǎng)站維護(hù)、建站公司、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容