C語言結構體指針 删除函數 為什麼删除後會顯示0? 麻煩各位大神速度指點一下~~
答案:2 資訊版本:手機版
解決時間 2019-10-06 06:27
已解決
2019-10-05 23:56
void Delete() //删除資訊函數
{
FILE *fp;
fp=fopen("student.txt","r");
char choose;
char name[20];
p=s1;
int i=0;
fread(p,sizeof(Student),1,fp);
printf("請輸入删除學生姓名:");
scanf("%s",&name);
while(strcmp(name,p->Name)!=0&&p!=NULL) //判斷該學生資訊是否存在
p++;i++;
if(p!=NULL)
{printf("學号 姓名 性别 國文 數學 英語 體育 C語言 總分 名次\n");
printf("%-6d%6s%6s%6d%6d%6d%6d%6d%7d%6d\n",
p->Num,p->Name,p->Sex,p->Chinese,p->Math,p->English,p->PE,p->Clanguage,p->total,p->rank);
fp=fopen("student.txt","w");
printf("\n是否删除(Y/N)");
scanf("%s",&choose);
if(choose=='y'||choose=='Y')
{
for(i=i;i{s1[i]=s1[i+1];}
printf("删除成功!\n");
fwrite(p,sizeof(struct Student),1,fp);
}
}
else printf("沒有此人資訊。");
printf("\n1.傳回主菜單\n2.繼續删除");
scanf("%s",&choose);
if(choose=='1')
Menu();
else if(choose=='2')
Delete();
最後結果就變成了第三行那樣~~~
~~怎麼減少一個機關啊!!我的N是define定義了的
最佳答案
2019-10-06 00:12
void Delete() { //删除資訊函數
FILE *fp;
fp = fopen("student.txt","r");//預設以文本方式打開
char choose;
char name[20];
p = s1;
int i = 0;
fread(p,sizeof(Student),1,fp);// 塊狀讀取文本檔案是不合适的
printf("請輸入删除學生姓名:");
scanf("%s",&name);
while(strcmp(name,p->Name) && p) //判斷該學生資訊是否存在
p++;i++; if(p != NULL) {
printf("學号 姓名 性别 國文 數學 英語 體育 C語言 總分 名次\n"); printf("%-6d%6s%6s%6d%6d%6d%6d%6d%7d%6d\n",p->Num,p->Name,p->Sex, p->Chinese,p->Math,p->English,p->PE,p->Clanguage,p->total,p->rank);
fp = fopen("student.txt","w");//預設以文本方式打開,fp已經用作檔案student.txt,
printf("\n是否删除(Y/N)");
scanf("%s",&choose); // choose是char變量,應該用"%c"讀取
if(choose=='y'||choose=='Y') {
for(i = i;i < N;i++) {
s1[i] = s1[i + 1]; // 這裡删除的是第一條記錄,不一定是要删除的那一條
} printf("删除成功!\n");
fwrite(p,sizeof(struct Student),1,fp);// 塊狀寫文本檔案是不合适的
}
}
else printf("沒有此人資訊。");
printf("\n1.傳回主菜單\n2.繼續删除");
scanf("%s",&choose); // 還是老問題
if(choose=='1') Menu();
else if(choose=='2') Delete(); // 遞歸調用會出問題的,這兒必須改 //
.................
}
全部回答
1樓
2019-10-06 00:46
#includetypedef struct stpeople
{
char name[20];
char adr[20];
char tel[20];
};
stpeople stp[5];
void input()
{
int i;
printf("輸入五人資訊\n");
for(i =0;i <5;i++)
{
printf("輸入第%d個人的名字\n");
scanf("%s",st[i].name);
printf("輸入第%d個人的位址\n");
scanf("%s",st[i].adr);
printf("輸入第%d個人的電話\n");
scanf("%s",st[i].tel);
}
}
void print()
{
int i;
for(i =0;i <5;i++)
{
printf("姓名 : %s 電話: %s 位址: %s\n",st[i].name,st[i].adr,st[i].tel);
}
}
void main()
{
input();//輸入
print();//輸出
}
我要舉報
如果感覺以上資訊為低俗/不良/侵權的資訊,可以點下面連結進行舉報,我們會做出相應處理,感謝你的支援!
大家都在看
推薦資訊