I have created a linked list, that holds the name of colours (Hungarian) read from a file. What my program is supposed to, is to compare the s string with the contents of the file. If it matches, print out the corresponding name from the file. But it won't do a damn thing.
What could be the issue?
File contents:
zold piros sarga lila rozsaszin tukiszkek fekete feher narancs okkersarga szintelen
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
int getl(char s[], int lim) /*ez a függvény kéri be a beolvasandó karakterláncot*/
{
int c,i;
for(i = 0; i < lim && (c = getchar()) != '\n' && c!=EOF; i++)
s[i] = c;
s[i] = '\0'; // tömb lezárasa
while(c != '\n' && c!=EOF)
c=getchar(); // puffer ürítése
return i; // visszateresi értek: string hossza
}
struct szinek {
char szinek[MAX];
struct szinek *kov;
};
int main()
{
FILE *fp;
char s[MAX] = "zold";
fp = fopen("/home/dumika/Desktop/szinek.txt", "r");
if(!fp) return;
struct szinek *llist, *head = NULL, *prev = NULL;
while(fgets(s, MAX, fp)) {
if(!(llist = (struct szinek*)malloc(sizeof(struct szinek)))) break;
if(head) {
prev->kov = llist;
} else {
head = llist;
}
prev = llist;
llist->kov = NULL;
strcpy(llist->szinek, s);
}
llist = head;
while(llist != NULL)
{
if(!strcmp(llist->szinek, s))
{
printf("%s", llist->szinek);
}
llist = llist->kov;
}
}