我正在编写一个程序(用于班级作业),以将普通单词翻译为其等值的海盗(hi = ahoy)。从文本文件中读取单个单词并翻译 - C
我已经使用两个字符串数组创建了字典,现在正在尝试翻译一个input.txt文件并将其放入output.txt文件中。我可以写入输出文件,但它只能将翻译的第一个单词反复写入新行。
我已经做了很多阅读/冲刷,从我所知道的情况来看,使用fscanf()来读取我的输入文件并不理想,但我无法弄清楚什么是更好的函数。我需要逐字读取文件(用空格分隔)并读取每个标点符号。
输入文件:
Hi, excuse me sir, can you help
me find the nearest hotel? I
would like to take a nap and
use the restroom. Then I need
to find a nearby bank and make
a withdrawal.
Miss, how far is it to a local
restaurant or pub?
输出:嗨(46次,每次一个单独的行)
翻译功能:
void Translate(char inputFile[], char outputFile[], char eng[][20], char pir[][20]){
char currentWord[40] = {[0 ... 39] = '\0'};
char word;
FILE *inFile;
FILE *outFile;
int i = 0;
bool match = false;
//open input file
inFile = fopen(inputFile, "r");
//open output file
outFile = fopen(outputFile, "w");
while(fscanf(inFile, "%s1023", currentWord) == 1){
if(ispunct(currentWord) == 0){
while(match != true){
if(strcasecmp(currentWord, eng[i]) == 0 || i<28){ //Finds word in English array
fprintf(outFile, pir[i]); //Puts pirate word corresponding to English word in output file
match = true;
}
else {i++;}
}
match = false;
i=0;
}
else{
fprintf(outFile, &word);//Attempt to handle punctuation which should carry over to output
}
}
}
-09-26
bullinka