C 从文件中读取数字并保存在链表中
文件格式: 我必须阅读这些数字,有50行这样的线,以Xvalue的方式。。。。链接列表中的Y值。我读数字有困难。 我创建的链接列表如下所示:C 从文件中读取数字并保存在链表中,c,C,文件格式: 我必须阅读这些数字,有50行这样的线,以Xvalue的方式。。。。链接列表中的Y值。我读数字有困难。 我创建的链接列表如下所示: list L; node *p, *q; int i; L = (list *)malloc(sizeof(node)); L -> x = L -> y = 0; L -> nextx = L -> nexty = NULL; FILE *fp; fp = fop
list L;
node *p, *q;
int i;
L = (list *)malloc(sizeof(node));
L -> x = L -> y = 0;
L -> nextx = L -> nexty = NULL;
FILE *fp;
fp = fopen("points.txt", "r");
if (fp == NULL)
{
fprintf(stderr, "Error: unable to open file...\n");
}
p = L;
for (i=0; i<100; ++i)
{
q = (node *)malloc(sizeof(node));
q -> x = // store x values here
q -> y = // store y values here
q -> nextx = q -> nexty = NULL;
p -> nextx = p -> nexty = q;
p = q;
}
1您可以使用fscanf从文件中读取整数对。检查返回值以确保fscanf匹配2项
2您的链表处理错误。普通链表只有一个下一个指针
代码可以用多种方式编写。以下是一个例子:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct s_node
{
int x;
int y;
struct s_node* next;
} node;
void print_list(node* p)
{
printf("List contains:\n");
while (p)
{
printf("x=%d y=%d\n", p->x, p->y);
p = p->next;
}
}
node* new_node(int x, int y)
{
node* p = malloc(sizeof *p);
assert(p);
p->x = x;
p->y = y;
p->next = NULL;
return p;
}
void free_list(node* p)
{
while(p)
{
node* tmp = p;
p = p->next;
free(tmp);
}
}
node* read_list(FILE* fp)
{
int x, y;
node* head = NULL;
node* last = head;
// Repeat reading as long as fscanf matches 2 items
while(fscanf(fp, "%d %d", &x, &y) == 2)
{
if (last)
{
// Insert at end of list
last->next = new_node(x, y);
last = last->next;
}
else
{
// Special handling for first element in list
head = new_node(x, y);
last = head;
}
}
return head;
}
int main(void)
{
FILE* fp = stdin; // Here input is taken from standard in
// Use:
// fp = fopen("points.txt", "r");
// assert(fp);
//
// to read from a file
node* head = read_list(fp);
print_list(head);
free_list(head);
return 0;
}
只需fscanffp,%d%d,&q->x,&q->y;?L=列表*mallocsizeofnode;将list*赋值给具有list类型的变量看起来很奇怪。另外,为什么要分配的大小是一个节点,而缓冲区似乎用作列表?顺便问一下,什么是列表和节点?啊,需要检查读取是否成功,因为循环是100次,而文件只包含50行。这就像fscanffp、%d%d、&q->x、&q->y!=2{freeq;break;}。即使在打印错误后,您的程序仍将尝试读取文件:无法打开文件。。。。这将很危险。您需要发布列表和节点的定义。列表L的处理似乎是错误的,同时使用nextx和nexty似乎是错误的。通常,链表只有一个下一个指针。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct s_node
{
int x;
int y;
struct s_node* next;
} node;
void print_list(node* p)
{
printf("List contains:\n");
while (p)
{
printf("x=%d y=%d\n", p->x, p->y);
p = p->next;
}
}
node* new_node(int x, int y)
{
node* p = malloc(sizeof *p);
assert(p);
p->x = x;
p->y = y;
p->next = NULL;
return p;
}
void free_list(node* p)
{
while(p)
{
node* tmp = p;
p = p->next;
free(tmp);
}
}
node* read_list(FILE* fp)
{
int x, y;
node* head = NULL;
node* last = head;
// Repeat reading as long as fscanf matches 2 items
while(fscanf(fp, "%d %d", &x, &y) == 2)
{
if (last)
{
// Insert at end of list
last->next = new_node(x, y);
last = last->next;
}
else
{
// Special handling for first element in list
head = new_node(x, y);
last = head;
}
}
return head;
}
int main(void)
{
FILE* fp = stdin; // Here input is taken from standard in
// Use:
// fp = fopen("points.txt", "r");
// assert(fp);
//
// to read from a file
node* head = read_list(fp);
print_list(head);
free_list(head);
return 0;
}