I am trying to read from a file called data.txt. and store it into a LL which looks like this
Quanta 2
New 2
Ready 2
A 1 3
B 3 4
C 5 6
D 7 9
I would like to read that file, one line at a time, storing each line at a node in a LL, e.g A 1 3, will be at one node.
The desired layout is this
Name Value
Quanta 2
New 2
Ready 2
-------------------------
Process NUT AT
A 1 3
B 3 4
C 5 6
D 7 9
I have wrote the following code, which does read from the file and displays results for name and value correctly, however I do not understand how can I make it so it reads one line at a time and stores that one line into specific node.
This is what my code Currently displays:
Name | Value|
3 1
4 3
6 5
9 7
A 1
B 3
C 5
D 7
New 2
Quanta 2
Ready 2
I have tried to solve this for quiet a while now, I have officially hit a mental block, I would appreciate any help you can offer.
Code:
#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;
}