从文本文件中读出单个单词并翻译-C

从文本文件中读出单个单词并翻译-C,c,file-io,c-strings,C,File Io,C Strings,我正在编写一个程序(用于课堂作业),将普通单词翻译成海盗对应词(hi=ahoy) 我已经使用两个字符串数组创建了字典,现在正试图翻译一个input.txt文件并将其转换为output.txt文件。我能够写入输出文件,但它只会将翻译后的第一个单词反复写入新行 我已经做了大量的阅读/整理工作,从我所知道的情况来看,使用fscanf()来阅读我的输入文件并不理想,但我无法找到更好的函数。我需要逐字(用空格分隔)阅读文件,还需要阅读每个标点符号 输入文件: Hi, excuse me sir, can

我正在编写一个程序(用于课堂作业),将普通单词翻译成海盗对应词(hi=ahoy)

我已经使用两个字符串数组创建了字典,现在正试图翻译一个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?
输出:ahoy(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


    }

}

}
void Translate(char-inputFile[],char-outputFile[],char-eng[][20],char-pir[][20]){
字符currentWord[40]={[0…39]='\0'};
字符词;
文件*填充;
文件*输出文件;
int i=0;
布尔匹配=假;
//打开输入文件
inFile=fopen(输入文件,“r”);
//打开输出文件
outFile=fopen(outputFile,“w”);
而(fscanf(infle,“%s1023”,currentWord)=1){
if(ispunct(currentWord)==0){
while(匹配!=真){

如果(strcasecmp(currentWord,eng[i])==0 | | i当您开始匹配不同的英语单词时,
i这里有一个代码片段,我用来解析内容。它的作用如下:

鉴于这一投入:

hi, excuse me sir, how are you.
它根据DELIMS常量将每个单词放入字符串数组,并删除DELIMS常量中的所有字符。但这会破坏原始输入字符串。我只需打印字符串数组:

[hi][excuse][me][sir][how][are][you][(null)]

这是从STDIN中输入的,但是您可以将它更改为从文件流中获取它。您也可能需要考虑输入限制等。

#include <stdio.h>
#include <string.h>
#define CHAR_LENGTH         100

const char *DELIMS = " ,.\n";
char *p;
int i;

int parse(char *inputLine, char *arguments[], const char *delimiters)
{
    int count = 0;
    for (p = strtok(inputLine, delimiters); p != NULL; p = strtok(NULL, delimiters))
    {
        arguments[count] = p;
        count++;
    }
    return count;
}

int main()
{
    char line[1024];
    size_t bufferSize = 1024;

    char *args[CHAR_LENGTH];

    fgets(line, bufferSize, stdin);
    int count = parse(line, args, DELIMS); 
    for (i = 0; i <= count; i++){ 
        printf("[%s]", args[i]); 
    } 
}
#包括
#包括
#定义字符长度100
常量字符*DELIMS=“,。\n”;
char*p;
int i;
int解析(字符*输入行,字符*参数[],常量字符*分隔符)
{
整数计数=0;
for(p=strtok(输入行,分隔符);p!=NULL;p=strtok(NULL,分隔符))
{
参数[count]=p;
计数++;
}
返回计数;
}
int main()
{
字符行[1024];
大小\u t bufferSize=1024;
char*args[char_LENGTH];
fgets(线路、缓冲区大小、标准输入);
int count=parse(行、参数、DELIMS);

对于(i=0;我非常感谢!这马上就解决了问题。另外,感谢您解释了我的代码不起作用的原因。希望我以后可以避免这个错误。
#include <stdio.h>
#include <string.h>
#define CHAR_LENGTH         100

const char *DELIMS = " ,.\n";
char *p;
int i;

int parse(char *inputLine, char *arguments[], const char *delimiters)
{
    int count = 0;
    for (p = strtok(inputLine, delimiters); p != NULL; p = strtok(NULL, delimiters))
    {
        arguments[count] = p;
        count++;
    }
    return count;
}

int main()
{
    char line[1024];
    size_t bufferSize = 1024;

    char *args[CHAR_LENGTH];

    fgets(line, bufferSize, stdin);
    int count = parse(line, args, DELIMS); 
    for (i = 0; i <= count; i++){ 
        printf("[%s]", args[i]); 
    } 
}