gpt4 book ai didi

C - 循环和文本文件

转载 作者:行者123 更新时间:2023-11-30 16:36:32 27 4
gpt4 key购买 nike

我必须编写一个程序,该程序将根据另一个文本文件中的词典更改一个文本文件中的单词。例如在“test.txt”中我有:

“妈妈 poszla z tata na zakupy”

在“slownik.txt”中我有:

“妈妈:妈妈,塔塔:父亲,巴布西亚:奶奶,不:开,”

我希望我的程序显示“mother poszla zfather on zakupy”,但只有第一个单词发生了变化。下面是我的 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 - 循环和文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com