C语言,建立了文件,向文件中输入了数据,但不知道为什么输出时显示不出来??可以帮忙看看代码哪里有问题吗??谢谢大神们!!

up___lqw 2020-10-10 04:50:11
以链表形式向文件输入数据,建立信息,然后想着写个输出函数看看是否输入
进去了,结果编译运行什么也没有显示。但编译通过没问题,不知道代码哪里
出来问题,想请教各位大神们帮忙看看代码,看看哪里有什么问题。时间有点
急!!在此谢谢大家啦!!!!
#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);

}
...全文
1141 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
up___lqw 2020-10-18
  • 打赏
  • 举报
回复
引用 6 楼 自信男孩的回复:
[quote=引用 5 楼 up___lqw 的回复:][quote=引用 4 楼 自信男孩的回复:][quote=引用 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的时候都没加)[/quote]
fscanf和scanf是一样的,只是fscanf指定了读取内容的文件。scanf如果加,fscanf也是要加&的~[/quote]
知道啦,谢谢!还能再问一个别的问题吗?就是刚开始在创立文件前我先创建了一个空的链表头,链表头和头指针这个为什么可以直接在文件里使用啊??不需要在打开文件后创建吗??今天突然发现这个不明白了。万分感谢!![/quote]
个人认为,链表创建一般是在读文件的时候创建的,而楼主是相当于在写文件的时候创建了(但是没有使用链表),正好这些创建的节点在下面的读文件操作里使用了,也是可以的。[/quote] 可不可以帮我看一下代码呢?感觉传入的函数参数没有用呢,就是调用函数时,在这个函数中沿头结点找对应的节点位置,不知道为什么找不到😢,看了好几遍都不知道问题出在哪里。编译是没有问题的,就是运行出了bug。如果可以的话,我把代码私发给您!!真的太太太感谢啦!!大作业真的蛮重要的!
up___lqw 2020-10-17
  • 打赏
  • 举报
回复
引用 6 楼 自信男孩的回复:
[quote=引用 5 楼 up___lqw 的回复:][quote=引用 4 楼 自信男孩的回复:][quote=引用 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的时候都没加)[/quote]
fscanf和scanf是一样的,只是fscanf指定了读取内容的文件。scanf如果加,fscanf也是要加&的~[/quote]
知道啦,谢谢!还能再问一个别的问题吗?就是刚开始在创立文件前我先创建了一个空的链表头,链表头和头指针这个为什么可以直接在文件里使用啊??不需要在打开文件后创建吗??今天突然发现这个不明白了。万分感谢!![/quote]
个人认为,链表创建一般是在读文件的时候创建的,而楼主是相当于在写文件的时候创建了(但是没有使用链表),正好这些创建的节点在下面的读文件操作里使用了,也是可以的。[/quote] 好的,太感谢你啦!!抱歉这么久才回复~ 其实这个程序是我们要写的大作业的一部分,然后现在我的代码刚刚全部写好,也通过编译了,但是有个地方出了一个bug,已注册的用户找不到信息,我也不知道哪里出了问题。代码行数也有点多,不太方便放到这个上面。如果您有时间,不嫌我麻烦的话,可以把代码私发给您,然后帮我看看吗??如果可以的话真是太感谢了!!(实在找不到人帮忙看看哪里有问题了
自信男孩 2020-10-12
  • 打赏
  • 举报
回复
引用 5 楼 up___lqw 的回复:
[quote=引用 4 楼 自信男孩的回复:][quote=引用 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的时候都没加)[/quote]
fscanf和scanf是一样的,只是fscanf指定了读取内容的文件。scanf如果加,fscanf也是要加&的~[/quote]
知道啦,谢谢!还能再问一个别的问题吗?就是刚开始在创立文件前我先创建了一个空的链表头,链表头和头指针这个为什么可以直接在文件里使用啊??不需要在打开文件后创建吗??今天突然发现这个不明白了。万分感谢!![/quote]
个人认为,链表创建一般是在读文件的时候创建的,而楼主是相当于在写文件的时候创建了(但是没有使用链表),正好这些创建的节点在下面的读文件操作里使用了,也是可以的。
up___lqw 2020-10-11
  • 打赏
  • 举报
回复
引用 4 楼 自信男孩的回复:
[quote=引用 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的时候都没加)[/quote]
fscanf和scanf是一样的,只是fscanf指定了读取内容的文件。scanf如果加,fscanf也是要加&的~[/quote] 知道啦,谢谢!还能再问一个别的问题吗?就是刚开始在创立文件前我先创建了一个空的链表头,链表头和头指针这个为什么可以直接在文件里使用啊??不需要在打开文件后创建吗??今天突然发现这个不明白了。万分感谢!!
自信男孩 2020-10-10
  • 打赏
  • 举报
回复
引用 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的时候都没加)[/quote]
fscanf和scanf是一样的,只是fscanf指定了读取内容的文件。scanf如果加,fscanf也是要加&的~
up___lqw 2020-10-10
  • 打赏
  • 举报
回复
引用 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的时候都没加)
up___lqw 2020-10-10
  • 打赏
  • 举报
回复
引用 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语句,然后也没有输出,怎么回事不知道
自信男孩 2020-10-10
  • 打赏
  • 举报
回复
#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);

}


供参考~

没有任何输出,那么是因为楼主没有输出。

代码的其他问题,详见上面的代码;

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧