以鏈表形式向檔案輸入資料,建立資訊,然后想著寫個輸出函式看看是否輸入
進去了,結果編譯運行什么也沒有顯示。但編譯通過沒問題,不知道代碼哪里
出來問題,想請教各位大神們幫忙看看代碼,看看哪里有什么問題。時間有點
急!!在此謝謝大家啦!!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct acc_message //定義結點的結構
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
} ;
int main()
{
struct acc_message *head; //創建空鏈表
head =(struct acc_message*)malloc(sizeof (struct acc_message));
head -> next =NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有銀行客戶資訊檔案
if ((fp = fopen("message.txt","wt+"))==NULL){
printf("Open this file error,press any key exit!/n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //創建新節點
if (p==NULL) break;
switch (i)
{ case 0:
fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
r=r->next;
}
fclose(fp);
}
uj5u.com熱心網友回復:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct acc_message //定義結點的結構
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
};
int main()
{
struct acc_message *head; //創建空鏈表
head =(struct acc_message *)malloc(sizeof (struct acc_message));
head-> next = NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有銀行客戶資訊檔案
if ((fp = fopen("message.txt","wt+"))==NULL){
//printf("Open this file error,press any key exit!/n ");
printf("Open this file error,press any key exit!\n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //創建新節點
if (p==NULL) break;
switch (i)
{
case 0:
//fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
fprintf (fp,"Jane 62221111 123456 8000\n");//, p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
//fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
fscanf(fp,"%s %d %d %lf\n",r->name, &r->cardnum, &r->password, &r->acc_balance);
r=r->next;
}
r = head->next;
while (r) {
fprintf(stdout,"%s %d %d %lf\n", r->name, r->cardnum, r->password, r->acc_balance);
r = r->next;
}
fclose(fp);
}
供參考~
沒有任何輸出,那么是因為樓主沒有輸出。
代碼的其他問題,詳見上面的代碼;
uj5u.com熱心網友回復:
參考 1 樓 自信男孩的回復: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct acc_message //定義結點的結構
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
};
int main()
{
struct acc_message *head; //創建空鏈表
head =(struct acc_message *)malloc(sizeof (struct acc_message));
head-> next = NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有銀行客戶資訊檔案
if ((fp = fopen("message.txt","wt+"))==NULL){
//printf("Open this file error,press any key exit!/n ");
printf("Open this file error,press any key exit!\n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //創建新節點
if (p==NULL) break;
switch (i)
{
case 0:
//fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
fprintf (fp,"Jane 62221111 123456 8000\n");//, p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
//fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
fscanf(fp,"%s %d %d %lf\n",r->name, &r->cardnum, &r->password, &r->acc_balance);
r=r->next;
}
r = head->next;
while (r) {
fprintf(stdout,"%s %d %d %lf\n", r->name, r->cardnum, r->password, r->acc_balance);
r = r->next;
}
fclose(fp);
}
供參考~
沒有任何輸出,那么是因為樓主沒有輸出。
代碼的其他問題,詳見上面的代碼;
fscanf函式不是用來輸出的嗎??我看您寫的后面輸出串列上后三個加上了&,這是為什么呢?(小白不懂就問),還有我現在在這個后面(關閉檔案后)寫printf陳述句,然后也沒有輸出,怎么回事不知道
uj5u.com熱心網友回復:
參考 1 樓 自信男孩的回復: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct acc_message //定義結點的結構
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
};
int main()
{
struct acc_message *head; //創建空鏈表
head =(struct acc_message *)malloc(sizeof (struct acc_message));
head-> next = NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有銀行客戶資訊檔案
if ((fp = fopen("message.txt","wt+"))==NULL){
//printf("Open this file error,press any key exit!/n ");
printf("Open this file error,press any key exit!\n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //創建新節點
if (p==NULL) break;
switch (i)
{
case 0:
//fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
fprintf (fp,"Jane 62221111 123456 8000\n");//, p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
//fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
fscanf(fp,"%s %d %d %lf\n",r->name, &r->cardnum, &r->password, &r->acc_balance);
r=r->next;
}
r = head->next;
while (r) {
fprintf(stdout,"%s %d %d %lf\n", r->name, r->cardnum, r->password, r->acc_balance);
r = r->next;
}
fclose(fp);
}
供參考~
沒有任何輸出,那么是因為樓主沒有輸出。
代碼的其他問題,詳見上面的代碼;
我知道啦,fscanf是讀取并沒有輸出,但是我看您后來改的后三個多加了&是為什么呢??而第一個name沒有加,這點想不太明白,請您指教!(因為我想fprintf的時候都沒加)
uj5u.com熱心網友回復:
參考 3 樓 up___lqw 的回復: Quote: 參考 1 樓 自信男孩的回復: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct acc_message //定義結點的結構
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
};
int main()
{
struct acc_message *head; //創建空鏈表
head =(struct acc_message *)malloc(sizeof (struct acc_message));
head-> next = NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有銀行客戶資訊檔案
if ((fp = fopen("message.txt","wt+"))==NULL){
//printf("Open this file error,press any key exit!/n ");
printf("Open this file error,press any key exit!\n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //創建新節點
if (p==NULL) break;
switch (i)
{
case 0:
//fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
fprintf (fp,"Jane 62221111 123456 8000\n");//, p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000\n");//,p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
//fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
fscanf(fp,"%s %d %d %lf\n",r->name, &r->cardnum, &r->password, &r->acc_balance);
r=r->next;
}
r = head->next;
while (r) {
fprintf(stdout,"%s %d %d %lf\n", r->name, r->cardnum, r->password, r->acc_balance);
r = r->next;
}
fclose(fp);
}
供參考~
沒有任何輸出,那么是因為樓主沒有輸出。
代碼的其他問題,詳見上面的代碼;
我知道啦,fscanf是讀取并沒有輸出,但是我看您后來改的后三個多加了&是為什么呢??而第一個name沒有加,這點想不太明白,請您指教!(因為我想fprintf的時候都沒加)
fscanf和scanf是一樣的,只是fscanf指定了讀取內容的檔案。scanf如果加,fscanf也是要加&的~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/166911.html
標籤:C語言
上一篇:Java傳輸檔案使用Base64優化傳輸速率。
下一篇:倆電腦互 ping 不通