我必须编写一个程序,该程序将从一个基本基于字典的文本文件中的单词更改为另一个文本文件。例如在“ test.txt”中,我有:
“妈妈poszla z tata na zakupy”
在“ slownik.txt”中,我有:
“妈妈:妈妈,
塔塔:父亲,
巴比西亚:奶奶,
na:on,”
我希望在我的程序中显示“ zakupy的母亲poszla z父亲”,但是只更改了第一个单词。在我的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;
}
}
}
请帮忙。
最佳答案
对于新手来说,使用字符串工作并不是用C语言完成的一项非常简单的工作。为了获得良好的编程效果,请首先写下您的要求,然后从中生成算法。算法准备就绪后,就可以开始基于该算法进行编码了。如果我查看您的代码,大多数情况下,您只是命中并尝试解决您的问题。这不仅会给您带来更多麻烦,还会使您感到沮丧。请参阅下面的我的程序,并与您的代码进行比较,找出您的错误。希望您以后能遵循我的建议。
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);
}
也使用下面的头文件
stdio.h,stdlib.h,string.h
关于c - C-循环和文本文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48403980/