I have a dynamic structure linked list and i need to create functions that:
1. will load data from text file.
2. add new element to my linked list structure.
3. output all data existing in my linked list.
I also have menu, so i call these functions to input what i need in console.
I guess that i have problems with dynamic data allocation and pointers.
https://i.sstatic.net/QJIYA.jpg
I've struggling with this already couple days trying solve problem with a proper allocate pointers.
#define MES_LEN 100
#define m 23
typedef struct {
char index[m];
char name[MES_LEN];
char release[MES_LEN];
char length[MES_LEN];
char artist[MES_LEN];
} INFORM;
typedef struct list_elem {
INFORM inform;
struct list_elem *next;
}
LEL;
LEL* list;
void AddElement(void); // Writing new element to file (DATAinput)
void LoadData(void); // Reading previously written data
void ReadElement (void); // Reading data from existing file data (DATA)
void OutpuAll(void); // Output in console all previously added data from both files
int menu(void);
int main() {
...
case 1: OutpuAll(); break; // Output all data
case 2: AddElement(); ReadElement(); break; // Add element to file
case 3: LoadData(); break; // Loading data from the file
...
}
void AddElement (void) // I have an empty file that i fill and then read and link to my main structure(this works fine)
{
char albN[25], albR[11], albL[11], albA[25], albI[5];
FILE * fileD;
fileD = fopen("DATAinput.txt", "r");
...
printf("Enter album artist \n");
scanf("%s", albA);
fprintf(fileD, "%s ", albA);
fclose(fileD);
}
void ReadElement (void) // Reading previously written data(AddElement)
{
LEL *ptr;
ptr = (LEL*)malloc(sizeof(LEL));
FILE * fileD;
fileD = fopen("DATAinput.txt", "r");
if (fileD == NULL) {
printf("Can not open the file\n");
exit(1);
}
while(fscanf (fileD, "%s %s %s %s %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist) != EOF)
{ ptr->next=NULL;
printf("\n Id:%s Album: %s Released: %s Length: %s Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist); }
fclose(fileD);
free(ptr);
}
void LoadData(void)
// Reading existing data from file
{
LEL *current, *head;
LEL *ptr;
ptr = (LEL*)malloc(sizeof(LEL));
FILE * file;
file = fopen("DATA.txt", "r");
if (file == NULL) {
printf("Can not open the file\n");
}
while(fscanf (file, "%s %s %s %s %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist) != EOF)
{ printf("\n %s Album: %s Released: %s Length: %s Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist); }
fclose(file);
ptr->next = NULL;
}
void OutpuAll(void) // Trying to output data that i previously loaded from files to structure.
{
LEL *ptr;
LEL* ptr = list;
while (ptr!=NULL) {
printf("\n Id:%s Album: %s Released: %s Length: %s Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist);}
}
ptr = ptr->next;
}
I have some troubles with pointers at the end of these functions
void ReadElement (void)
void LoadData(void)
void OutpuAll(void)
I will be grateful for help