简体   繁体   English

C-循环和文本文件

[英]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: 例如在“ test.txt”中,我有:

"mama poszla z tata na zakupy" “妈妈poszla z tata na zakupy”

and in "slownik.txt" i have: 在“ slownik.txt”中,我有:

"mama:mother, tata:father, babcia:grandma, na:on," “妈妈:母亲,塔塔:父亲,巴比西亚:祖母,na:上,”

I expected to my program disply "mother poszla z father on zakupy", but only first word is changed. 我希望在我的程序中显示“ zakupy的母亲poszla z父亲”,但是只更改了第一个单词。 Below my code fragment in C: 在我的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. 对于新手来说,使用字符串工作不是用c语言编写的一项非常简单的工作。 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 stdio.h,stdlib.h,string.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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