0

I must write a program which will be changing a words from one text file basic on dictionary from another text file. For example in "test.txt" i have:

"mama poszla z tata na zakupy"

and in "slownik.txt" i have:

"mama:mother, tata:father, babcia:grandma, na:on,"

I expected to my program disply "mother poszla z father on zakupy", but only first word is changed. Below my code fragment in C:

char *token; 
int k = 0;

while (!feof(slownik)) //
{
    k = 0;
    fscanf(slownik,"%s",&liniatekstu);
    token = strtok(liniatekstu," ,.:");

    while(token != NULL)
    {
       tab[k] = token;
     //  printf("%s\n", tab[k]);
       token = strtok(NULL," ,.:");
       k = k + 1;
    }
    char c;
    char slowo[1000];
    int idx = 0;

    while(!feof(fp))
    {
        c = fgetc(fp); // get sign
        if( ! isspace(c) )
        { // full sign - add to word
            slowo[idx++] = c;
            if(idx>=1000)
            {
                printf("Error - word has > 1000 signs\n");
            }
        }
        else 
        { // blank sign - end of word
        //  if(idx == 0) // idx=0 word is empty
            //  continue;
                // we have final word 
                // - add zero to end of word and display to screen 
            slowo[idx] = 0;
             // printf("%s\n", slowo);
            // TU MAM SLOWO
            const char* x = tab[0]; // polish version of word
            const char* y = tab[1]; // english version of word

            if ( strcmp(slowo,x)  == 0) // comparation word from "test.txt" and "slownik.txt" if its the same display english version of word
            {
                printf("%s ",y);
            }
            else
            {
                printf("%s ",slowo); // display polish version
            }
            idx = 0;
         }
    }
}

Please help.

P.Manek
  • 11
  • 3
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Lundin Jan 23 '18 at 14:26
  • 1
    Using `while (!feof(go))` is wrong. You should probably not use nested loops. You should have one loop that reads the dictionary into memory and then a second loop that reads and translates the text file. – Jonathan Leffler Jan 23 '18 at 14:33
  • 1
    And please code in english if you want other people to look at your code. Having variable names one can't figure out the meaning doesn't help in the review. – liberforce Jan 23 '18 at 14:51
  • 1
    Welcome to Stack Overflow! Try making this into a [mcve], please! <3 – SIGSTACKFAULT Jan 23 '18 at 16:18
  • @liberforce: that could be a useful function for this program (if it can be got to work). – Jongware Jan 23 '18 at 18:47
  • @P.Manek, Can you please confirm whether my below solution working for you or still you are facing problem. – Abhijit Pritam Dutta Jan 24 '18 at 11:41
  • It don`t work. Program crashed. The error is "Access violation" so I think this is problem with memory but I don`t know how to fix your code :/ – P.Manek Jan 24 '18 at 14:26
  • When I changed strcat() function to strcpy() the last word in the file is changing but rest is not changed. – P.Manek Jan 25 '18 at 11:14

1 Answers1

0

Working with string is not a very easy work in c language for newcomer. For good programming first write down your requirement and then generate an algorithm from it. Once your algorithm is ready start coding based on that algorithm. If I look into your code you are most of the time just doing hit and try to fix your problem. This will not only creating more trouble for you but also raise frustrating as well. See my program below and compare with your code and find out you mistakes. Hope you will following my advice in future.

   void main()
   {
            FILE *fpointer_s, *fpointer_d;
            fpointer_s = fopen("test.txt","r");
            fpointer_d = fopen("slownik.txt","r");
            if(fpointer_s != NULL && fpointer_d != NULL)
            {
                    printf("Dictionary job starting....\n");
            }
            else
            {
                    printf("File does not exist....\n");
                    return;
            }
            //FILEs are OPENED
            char line[255];
            char dictionary[1025];//for dictionary file
            char text[1025];//for text file
            char delim[2]=" ";
            memset(text,0,sizeof(text));
            while(!feof(fpointer_d) && fgets(line,sizeof line,fpointer_d))
            {
                    strcat(dictionary,line);//we are loading the dictionary here
            }
            memset(line,0,sizeof(line));//clear line to read next file
            //now read the next file line by line
            while(!feof(fpointer_s) && fgets(line,sizeof line,fpointer_s))
            {
                char *word = strtok(line,delim);
                do
                {
                  char *found = strstr(dictionary,word);//check if the word available in dictionary
                  char tword[20];//variable to store translated word
                  int i = 0;
                  if (found)//if the word found in dictionary use the translated word i.e. tword
                  {
                    found = found + strlen(word)+1;//pointing to the English equivalent
                    memset(tword,0,sizeof(tword));//clear previous value
                    while(*found !=',' && *found !='\n' && *found !=NULL )//copy character by character till end of English word
                      tword[i++] = *found++;
                    tword[i]=0;//assign end of string character
                     if(strlen(text)> 0)
                        strcat(text," ");
                     strcat(text,tword);
                   }//end if
                   else//if word not found in dictionary just add the original word
                   {
                     if(strlen(text)> 0)
                      strcat(text," ");
                     strcat(text,word);
                   }
                  word = strtok(NULL,delim);
                }while(word);
            }
             //finally we translated the text into english
             printf("%s\n",text);
    }

Also use below header files as well

stdio.h,stdlib.h,string.h

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17