C语言用文件输出链表信息出现乱码

/*wkrs.h*/#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>#def... /*wkrs.h*/
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <string.h>

# define LEN_NAME 20
# define LEN_NUM 20

typedef struct Node {
char name[LEN_NAME];
char num[LEN_NUM];
int sex;
int age;
struct Node * link;
} wkrs;

wkrs * init(void);
void readin(wkrs * first);
void display(void);

/*_main.c*/
# include "wkrs.h"

int main() {
wkrs * first;
first = init();
readin(first);
display();
return 0;
}
/*readin.c*/
# include "wkrs.h"

void readin(wkrs * first) {
int ok = 1;
char c, _c;
wkrs * p, * q = first;
FILE * fp;
while(ok) {
printf("Please input your information:\n");
p = (wkrs*)malloc(sizeof(wkrs));
memset(p->name, 0, sizeof(p->name)); memset(p->num, 0, sizeof(p->num));
printf("Please input your name:");
scanf("%s", p->name);
printf("Please input your number:");
scanf("%s", p->num);
printf("Please input your age:");
scanf("%d", &p->age);
printf("Please choose:\n0.Male\t1.Female\n");
scanf("%d", &p->sex);
printf("--------INPUT FINISH--------\n");
printf("Please enter your information:\n");
printf("Name:%s\n", p->name);
printf("Number:%s\n", p->num);
printf("Age:%d\n", p->age);
printf("Sex:%s\n", p->sex ? "Female" : "Male");
printf("Are you sure?(Y/N)\n");
_c = getchar();
if((c = getchar()) == 'Y') {
q = q->link = p;
q->link = NULL;
}
else {
printf("Please input again!\n");
continue;
}
_c = getchar();
printf("Go on inputting?(Y/N)\n");
ok = ((c = getchar()) == 'Y') ? 1 : 0;
}
fp = fopen("worker.txt", "wb");
for(p = first->link; p != NULL; p = p->link)
fwrite(p, sizeof(wkrs), 1, fp);
fclose(fp);
}

/*display.c*/

# include "wkrs.h"

void display(void) {
int n = 0;
FILE * fp;
wkrs * p;
printf("----------WORKER INFORMATION-----------\n\n");
fp = fopen("worker.txt", "rb");
while(!feof(fp)) {
p = (wkrs*)malloc(sizeof(wkrs));
fread(p, sizeof(wkrs), 1, fp);
printf("------------------------------------\n");
printf("Name:%s\nNumber:%s\nAge:%d\nSex:%s\n", p->name, p->num, p->age, p->sex ? "Female" : "Male");
printf("------------------------------------\n");
n++;
}
printf("A total of %d people\n", n);
printf("--------OUTPUT FINISH--------\n\n");
fclose(fp);
}
展开
 我来答
fhtsnb
2012-05-05
知道答主
回答量:12
采纳率:0%
帮助的人:15.4万
展开全部
在VC里,只有当文件位置指针(fp->_ptr)到了文件末尾,然后再发生读/写操作时,标志位(fp->_flag)才会被置为含有_IOEOF。然后再调用feof(),才会得到文件结束的信息。
因此,如果运行如下程序:
char c;
while(!feof(fp))
{
c = fgetc(fp);
printf("%X\n", c);
}
会发现多输出了一个FF,原因就是在读完最后一个字符后,fp->flag仍然没有被置为_IOEOF,因而feof()仍然没有探测到文件结尾。直到再次调用fgetc()执行读操作,feof()才能探测到文件结尾。这样就多输出了一个-1(即FF)。
正确的写法应该是:
char c;
c = fgetc(fp);
while(!feof(fp))
{
printf("%X\n", c);
c = fgetc(fp);
}

参考资料: http://zhidao.baidu.com/question/302828077.html

sunyqg
2012-05-05 · TA获得超过1084个赞
知道小有建树答主
回答量:477
采纳率:75%
帮助的人:634万
展开全部
没什么太大的问题,这句需要检查一下:
fread(p, sizeof(wkrs), 1, fp);
fread要判断是否读出了数据,可改为:
int nread;
...
nread = fread(p, sizeof(wkrs), 1, fp);
if( nread ){
printf("------------------------------------\n");
printf("Name:%s\nNumber:%s\nAge:%d\nSex:%s\n", p->name, p->num, p->age, p->sex ? "Female" : "Male");
printf("------------------------------------\n");
n++;
}
...
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xtjmdw
2012-05-05 · TA获得超过278个赞
知道小有建树答主
回答量:394
采纳率:0%
帮助的人:236万
展开全部
判断文件结束的地方有些问题。feof是表示最后一次的读操作是否成功。

void display(void) {
int n = 0;
FILE * fp;
wkrs * p;
printf("----------WORKER INFORMATION-----------\n\n");
fp = fopen("worker.txt", "rb");
p = (wkrs*)malloc(sizeof(wkrs));
if(!feof(fp))
fread(p, sizeof(wkrs), 1, fp);
while(!feof(fp)) {

printf("------------------------------------\n");
printf("Name:%s\nNumber:%s\nAge:%d\nSex:%s\n", p->name, p->num, p->age, p->sex ? "Female" : "Male");
printf("------------------------------------\n");
fread(p, sizeof(wkrs), 1, fp);
n++;
}
printf("A total of %d people\n", n);
printf("--------OUTPUT FINISH--------\n\n");
fclose(fp);
}

参考资料: http://baike.baidu.com/view/656648.htm

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式