i have created linked list, holds name of colours (hungarian) read file. program supposed to, compare s string contents of file. if matches, print out corresponding name file. won't damn thing.
what 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 függvény kéri beolvasandó karakterláncot*/ { int c,i; for(i = 0; < 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; } }
your program works fine me. suspect combination of 2 things confusing you:
you initialize buffer
s
"zold"
, use buffer read in lines of file, thereby overwriting initial contents. when later scan linked list, therefore, looking last string read, not"zold"
.if there newline after
szintelen
in data file last line program reads blank or empty. when program prints match, might not recognize bona fide correct output.
you verify program matching string adding text output emits in case. example:
if(!strcmp(llist->szinek, s)) { printf("found '%s'\n", llist->szinek); }
you can test specific string using string literal in comparison instead of s
, or creating separate array comparison value. example:
if(!strcmp(llist->szinek, "zold")) { printf("found '%s'\n", llist->szinek); }
for latter, not neglect follow advice in comments remove trailing newlines included fgets()
.