简体   繁体   中英

C - loop and text files

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM