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..

Recommended Answers

All 3 Replies

1. Use code tags properly:
[code=c] sources...

[/code]
2. Well, and WHAT's your problem?

1. Use code tags properly:
[code=c] sources...

[/code]
2. Well, and WHAT's your problem?

1. Yeah, my bad..i'll do it right the 2nd time.

2. My problem is - the program's not working :) i thought it might a code mistake, which is quite plain to see, but i can't see it because i haven't worked with files and arguments too much..you'll see what i mean if you paste the code, compile and run it i guess...wish i could be more specific..

Thank you for your reply.

1) struct L1 and struct L2. Since the structures are identical except for the names you could just one struct for both purposes. No need to declare two structures that do the same thing, just reuse one struct.

2) >> if(strcmp(a->string,b->string)) {

strcmp() returns 0 if the two strings are identical. That should be coded like this: if(strcmp(a->string,b->string) == 0) {

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.