[英]C - loop and text files
我必須編寫一個程序,該程序將從一個基本基於字典的文本文件中的單詞更改為另一個文本文件。 例如在“ test.txt”中,我有:
“媽媽poszla z tata na zakupy”
在“ slownik.txt”中,我有:
“媽媽:母親,塔塔:父親,巴比西亞:祖母,na:上,”
我希望在我的程序中顯示“ 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.