gpt4 book ai didi

c - 链接列表,卡在文件管理上

转载 作者:行者123 更新时间:2023-11-30 15:24:23 25 4
gpt4 key购买 nike

我正在尝试读取名为 data.txt 的文件。并将其存储到如下所示的 LL 中

Quanta 2
New 2
Ready 2
A 1 3
B 3 4
C 5 6
D 7 9

我想读取该文件,一次一行,将每一行存储在 LL 中的一个节点上,例如 A 1 3,将存储在一个节点上。

想要的布局是这样的

Name    Value
Quanta 2
New 2
Ready 2
-------------------------
Process NUT AT
A 1 3
B 3 4
C 5 6
D 7 9

我编写了以下代码,它确实从文件中读取并正确显示名称和值的结果,但是我不明白如何才能使其一次读取一行并将该行存储到特定的位置节点。

这是我的代码当前显示的内容:

姓名 |值|

3       1
4 3
6 5
9 7
A 1
B 3
C 5
D 7
New 2
Quanta 2
Ready 2

我已经尝试安静地解决这个问题有一段时间了,我已经正式遇到了心理障碍,我将不胜感激您能提供的任何帮助。

代码:

                #include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NAME_LENGTH 20
#define PROCESS_LENGTH 20



//create quanta structure.

typedef struct Quanta
{
char* name;
int Value;

struct Quanta *next;
}Quanta;


Quanta* new_Q(char*, int);
Quanta* insert_by_Q(Quanta*, Quanta*);
void print_list(Quanta*);

int main()
{
FILE *in;
char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
char filename[25];
int Value = 0;
//1. --------------Error Checking-----------------
printf("File name please:\n");
gets(filename);

in = fopen(filename, "rb");
if (in == NULL)
{
printf("The input file failed to open.\n");
printf("Program cannot continue. Exiting. . .\n");
return 1; //Exit Program
}
//2. ------Linked List Operations------
Quanta* head = NULL; //Create Empty Linked List
Quanta* current = NULL;
while(!feof(in)) //Check for file end
{
//Read first data value to kickstart.
if(fscanf(in, "%s %d ", name,&Value) == EOF)
{
break;
}

Quanta* hold = new_Q(name, Value);
head = insert_by_Q(head, hold);
}

//3. ------Print the new List------
print_list(head);
return 1; //Exit Success
}

Quanta* new_Q(char* name, int Value) {

//Create new Quanta and malloc space
Quanta* new = (Quanta*)malloc(sizeof(struct Quanta));
new->name = (char*)malloc(sizeof(char) * NAME_LENGTH);
//Set data
strcpy(new->name, name);
new->Value = Value;
new->next = NULL;
//Retun a pointer to the node
return new;

}

//Inserts new node into an alphabetically sorted linked list.
Quanta* insert_by_Q(Quanta* head, Quanta* new)
{
Quanta* current = NULL;
current = head;
if(current == NULL || strcmp(current->name, new->name) > 0)
{
new->next = current;
return new;
} else
{
while(current->next != NULL && strcmp(current->next->name, new->name) < 0)
{
current = current->next;
}
}
new->next = current->next;
current->next = new;
return head;

}


void print_list(Quanta* head)
{
Quanta* current;
current = head;
char p[] = "Name";
char c[] = "Value";


//Header
printf("\n\n|%10s | %10s| \n", p, c);
printf("-----------------------------------------------------------------------\n");


while(current != NULL)
{
printf("|%10s |%10d|\n", current->name, current->Value);
current = current->next;
}
printf("-----------------------------------------------------------------------\n");

return;
}

最佳答案

  typedef struct Quanta
{
char* name;
int Value;
int Value1; /* To hold the second integer from line 4 onwards */
struct Quanta *next;
} Quanta;

使用 fgets() 读取直到文件末尾,读取每一行后使用 sscanf() 获取所需的数据。我在这里假设为前 3 行存储 Value1=0,并且对于其余行,该字段已正确更新。

char buf[100];
char name[20];
int val=0,i=0,val1=0;
while(fgets(buf, sizeof(buf),in) != NULL)
{
if(i<=2)
{
if(sscanf(buf,"%s %d",&name,&val) == 2)
{
Quanta* hold = new_Q(name, val,0);
head = insert_by_Q(head, hold);
}
}
else
{
if(sscanf(buf,"%s %d %d",&name,&val,&val1) == 3)
{
Quanta* hold = new_Q(name, val,val1);
head = insert_by_Q(head, hold);
}
}
i++;
}

连同此修复

Quanta* new_Q(char*, int,int);

保存从第 4 行开始的额外值。

打印时请确保注意何时打印 val1、何时不打印。

关于c - 链接列表,卡在文件管理上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28435093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com