当前位置: 代码迷 >> C语言 >> [求助]谁告诉我,凭什么这样!?
  详细解决方案

[求助]谁告诉我,凭什么这样!?

热度:169   发布时间:2006-08-08 14:18:44.0
[求助]谁告诉我,凭什么这样!?

编译环境: TC2.0。下载版,无安装程序,解压了就能用的那种。

我的程序如下:
#include <stdio.h>
#define NULL 0
#define LEN sizeof(struct record)

struct record
{
char *name;
char *tel[3];
char *group;
struct record *next;
}

main()
{
struct record *head;
struct record *p1, *p2;
FILE *fp = fopen("F:\\records.txt","rt");
p1 = p2 = (struct record *) malloc(LEN);
if(fp == 0)
{
printf("Open file failure.");
}
else
{
fscanf(fp, "%s%s%s%s%s", p1->name, p1->tel[0], p1->tel[1], p1->tel[2], p1->group);
printf("hahh~First record is: %s-%s-%s-%s-%s\n", p1->name, p1->tel[0], p1->tel[1], p1->tel[2], p1->group);
if(strcmp(p1->name, "") == 1)
{
printf("No record!");
}
else
{
head = p1;
do
{
fscanf(fp, "%s%s%s%s%s", p1->name, p1->tel[0], p1->tel[1], p1->tel[2], p1->group);
if(strcmp(p1->name, "") == 1)
{
printf("No record or record is loaded!");
p2->next = NULL;
break;
}
printf("Now, the record is: %s-%s-%s-%s-%s\n", p1->name, p1->tel[0], p1->tel[1], p1->tel[2], p1->group);;
p1 = (struct record *) malloc(LEN);
p2->next = p1;
p2 = p1;
}while(feof(fp) == 0);
fclose(fp);
}
} /*
for(p1 = head; p1->next != NULL; p1 = p1->next)
{
printf("name: %s\ntel1: %s\ntel2: %s\ntel3: %s\ngroup: %s\n\n", p1->name, p1->tel[0], p1->tel[1], p1->tel[2], p1->group);
} */
getch();
}

F:\records.txt文件中的内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

可我运行了输出结果却是这样的:

hahh~First record is: 1-2-3-4-5
Now, the record is: 6-7-8-9-10
Now, the record is: 11-12-13-14-15
Now, the record is: <null>-<null>-18-19-20
Null pointer assignment


凭什么!?循环嘛!凭什么不一样!?凭什么有“null”的出现!?并且如果记录多了,null出现的位置都还会改变!是我的TC2.0 盗版!?


----------------解决方案--------------------------------------------------------
ni zai gei cheng xu jia mi ba

----------------解决方案--------------------------------------------------------
  相关解决方案