Hello, i found your forum useful from time to time while i was an absolute beginner in C language. And i'd like to thank you all for that. Now i have a problem i don't have too much time to solve, so i was wondering if any of you guys can help me with it. It's NOT homework, it's my own exam practice.
A user is supposed to give two .txt file names through the command line (char* argv[]), then the first .txt files' contents (strings, each in a new line) are supposed to be saved in a list. After that, the second files' contents (strings, formatted like in the 1st file) are supposed to be compared to the 1st files' list contents and the matches are to be deleted from the list containing the 1st files' contents. Example:
file1.txt:
i
want
to
learn
C
file2.txt:
i
C
Output:
want
to
learn
Here's the code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef struct L1 {
char string[MAX];
struct L1 *sledeci;
} L1;
typedef struct L2 {
char string[MAX];
struct L2 *sledeci;
} L2;
L1* izbaci(L1 *a, L2 *b, int brojac) {
L1* te=a, *pom;
while(brojac){
if(strcmp(a->string,b->string)) {
pom=te->sledeci;
free(te);
te=pom;
te=te->sledeci;
b=b->sledeci;
brojac--; } }
}
void stampaj(L1 *element) {
while(element!=NULL) {
printf("\n->%s", element->string);
element=element->sledeci; }
}
main(int argc, char *argv[]) {
int i,brojac=0;
char *string[MAX];
int slovo;
L1 *novi, *pocetak_liste;
L2 *novi2, *pocetak_liste2;
FILE *f1, *f2;
pocetak_liste=NULL;
pocetak_liste2=NULL;
for(i=1;i<argc;i++) {
f1=fopen(argv[i],"r");
while((slovo=fgetc(f1))!=EOF)
while((slovo=fgetc(f1))!='\n') {
fgets((char*)string,MAX,f1);
novi=(L1*)malloc(sizeof(L1));
puts(novi->string);
novi->sledeci=pocetak_liste;
pocetak_liste=novi;
}
fclose(f1);
i++;
f2=fopen(argv[i],"r");
while((slovo=fgetc(f2))!=EOF)
while((slovo=fgetc(f2))!='\n') {
fgets((char*)string,MAX,f2);
novi2=(L2*)malloc(sizeof(L2));
puts(novi2->string);
novi2->sledeci=pocetak_liste2;
pocetak_liste2=novi2;
brojac++;
}
fclose(f2); }
izbaci(pocetak_liste,pocetak_liste2,brojac);
stampaj(pocetak_liste);
system("pause");
}
Sorry for the variables and such in the code not being in english..it's not my native language..