C程序未接收所有输入

C程序未接收所有输入,c,redirect,input,C,Redirect,Input,我目前正在编写一个程序,该程序应该接收文本文件的输入,并输出有关文本的统计信息,如字母数、单词大小、出现频率以及每个单词出现的次数。但是,每次我运行程序时,它都不会读取所有文本。它只读开头几个字。以下是一些示例文本: 1 你好,我叫鲍勃 我住在加拿大 它将只显示“你好,我的名字是”。这似乎切断了最后一句话。我使用编译器中文本文件的输入重定向从文件导入文本(我必须这样做) 数字表示应该读取的行数。我应该如何纠正我的问题?我对编程非常陌生,所以我相信这是一些基本的东西 #include <s

我目前正在编写一个程序,该程序应该接收文本文件的输入,并输出有关文本的统计信息,如字母数、单词大小、出现频率以及每个单词出现的次数。但是,每次我运行程序时,它都不会读取所有文本。它只读开头几个字。以下是一些示例文本:

1
你好,我叫鲍勃
我住在加拿大

它将只显示“你好,我的名字是”。这似乎切断了最后一句话。我使用编译器中文本文件的输入重定向从文件导入文本(我必须这样做)

数字表示应该读取的行数。我应该如何纠正我的问题?我对编程非常陌生,所以我相信这是一些基本的东西

 #include <stdio.h>
 #include <string.h>


#define MAX_LINE_LENGTH 80
#define MAX_WORD_LENGTH 20
#define MAX_LINES 10

void letterAnalysis(char [][MAX_LINE_LENGTH], int lineTotal);
int wordLengthAnalysis(char [][MAX_LINE_LENGTH], int lineTotal, int wordLength);
void wordAnalysis(char [][MAX_LINE_LENGTH], int lineTotal);


int main (void){

    int lineTotal = 0;
    int wordSize = 0;
    char text[lineTotal][MAX_LINE_LENGTH];
    char n[1] = {0};

    fgets(n, 800, stdin);
    lineTotal = n[0] - '0';


    for(int i = 0; i < lineTotal; i++){}
        fgets(text[i], MAX_WORD_LENGTH, stdin);
    }

    printf("\n***Letter count analysis***\n");
    letterAnalysis(text, lineTotal);

    printf("\n***Word length analysis***\n");
    for (int i = 1; i <= MAX_WORD_LENGTH; i++){
        wordSize = wordLengthAnalysis(text, lineTotal, i);
        if (wordSize == 1){
            printf("\n%-2d\tword of length %d", wordSize, i);
        }
        else{
            printf("\n%-2d\twords of length %d", wordSize, i);
        }
    }

    printf("\n\n***Word analysis***\n");
    wordAnalysis(text, lineTotal);

    return 0;

}

void letterAnalysis(char text[][MAX_LINE_LENGTH], int lineTotal){

    int alphabet[26] = {0};

    for (int i = 0; i < lineTotal; i++){
        for(int j = 0; j < MAX_LINE_LENGTH; j++){
            switch(text[i][j]){
                case 'A': case 'a':
                alphabet[0]++;
                break;
                case 'B': case 'b':
                alphabet[1]++;
                break;
                case 'C': case 'c':
                alphabet[2]++;
                break;
                case 'D': case 'd':
                alphabet[3]++;
                break;
                case 'E': case 'e':
                alphabet[4]++;
                break;
                case 'F': case 'f':
                alphabet[5]++;
                break;
                case 'G': case 'g':
                alphabet[6]++;
                break;
                case 'H': case 'h':
                alphabet[7]++;
                break;
                case 'I': case 'i':
                alphabet[8]++;
                break;
                case 'J': case 'j':
                alphabet[9]++;
                break;
                case 'K': case 'k':
                alphabet[10]++;
                break;
                case 'L': case 'l':
                alphabet[11]++;
                break;
                case 'M': case 'm':
                alphabet[12]++;
                break;
                case 'N': case 'n':
                alphabet[13]++;
                break;
                case 'O': case 'o':
                alphabet[14]++;
                break;
                case 'P': case 'p':
                alphabet[15]++;
                break;
                case 'Q': case 'q':
                alphabet[16]++;
                break;
                case 'R': case 'r':
                alphabet[17]++;
                break;
                case 'S': case 's':
                alphabet[18]++;
                break;
                case 'T': case 't':
                alphabet[19]++;
                break;
                case 'U': case 'u':
                alphabet[20]++;
                break;
                case 'V': case 'v':
                alphabet[21]++;
                break;
                case 'W': case 'w':
                alphabet[22]++;
                break;
                case 'X': case 'x':
                alphabet[23]++;
                break;
                case 'Y': case 'y':
                alphabet[24]++;
                break;
                case 'Z': case 'z':
                alphabet[25]++;
                default: break;
            }       
        }
    }

    for(int i = 0; i <= 25; i++){
        printf("%c: \t%d\n", ('a' + i), alphabet[i]);;
    }
}

int wordLengthAnalysis(char text[][MAX_LINE_LENGTH], int lineTotal, int wordLength){

    int sentenceLength;
    int counter, wordSize = 0;

    for(int i = 0; i < lineTotal; i++){
        sentenceLength = strlen(&text[i][0]);
        for(int j = 0; j < sentenceLength + 2; j++){
            if(text[i][j] == ' '){
                if(counter == wordLength){
                    ++wordSize;
                    counter = 0;
                }
                else{
                counter = 0;
                }
            }
            else{
                counter++;
            }
        }
    }

    return wordSize;
}

void wordAnalysis(char text[][MAX_LINE_LENGTH], int lineTotal){

    char maxWords[800];
    char word[MAX_LINE_LENGTH], word2[MAX_WORD_LENGTH], *ptrText, *ptrTextCounter;
    int counter, textCounter = 0;
    int sentenceLength, wordTracker;
    int lineFlag;

    for(int i = 0; i < lineTotal; i++){
        ptrText = &text[i][0];
        sentenceLength = strlen(ptrText);
        counter = 0;

        for (int j = 0; j < sentenceLength + 1; j++){
            wordTracker = 1;

            if (text[i][j] == ' ' ){
                if (counter != 0){
                    sprintf(word, "%.*s", counter, ptrText);
                    ptrTextCounter = &text[i][j+1];
                    lineFlag = j;

                if(strstr(maxWords, word) == NULL){
                    for (int k = i; k < lineTotal; k++){
                        textCounter = 0;

                        if (lineFlag == j){
                            ptrTextCounter = &text[i][j+1];
                        }
                        else{
                            lineFlag = 0;
                            ptrTextCounter = &text[i][j+1];
                        }

                        for ( ; lineFlag < sentenceLength; lineFlag++){
                            if(text[k][lineFlag] == ' '){
                                if (textCounter != 0){
                                    if(textCounter == counter){
                                        sprintf(word2, "%.*s", textCounter, ptrTextCounter);
                                            if(strcmp(word, word2) == 0){
                                                wordTracker++;
                                            }
                                    }
                                    ptrTextCounter = &text[k][lineFlag];
                                    textCounter = 0;
                                }
                                else{
                                    ptrTextCounter = &text[k][lineFlag+1];
                                }
                            }
                            else{
                                textCounter++;
                            }
                        }
                    }

                    if(wordTracker == 1){
                        printf("\n\"%.*s\"\t\tappeared %d time", counter, ptrText, wordTracker);
                    }
                    else{
                        printf("\n\"%.*s\"\t\tappeared %d time", counter, ptrText, wordTracker);
                    }
                }


                strcat(maxWords, word);
                    ptrText = &text[i][j+1];
                    counter = 0;
                }
                else{
                    ptrText = &text[i][j+1];
                }
            }
            else{
                counter++;
            }
        }

    }


}
#包括
#包括
#定义最大直线长度80
#定义最大单词长度20
#定义最多10行
void letterAnalysis(字符[]最大行长],整数行总长度);
int-wordLength分析(char[][最大行长]、int-lineTotal、int-wordLength);
void wordAnalysis(char[][MAX_LINE_LENGTH],int lineTotal);
内部主(空){
int lineTotal=0;
int字号=0;
字符文本[lineTotal][MAX_LINE_LENGTH];
charn[1]={0};
fgets(n,800,stdin);
lineTotal=n[0]-“0”;
对于(int i=0;ifor(inti=0;i
这应该是最大线长度。

for(inti=0;i
这应该是最大线长度。

1)
int-lineTotal=0。。。字符文本[lineTotal][MAX_LINE_LENGTH]==>
字符文本[0][MAX_LINE_LENGTH]。在确定
lineTotal
后移动。2)
charn[1]={0};fgets(n,800,stdin)
800
的大小无效。它与实际大小不匹配(
1
)。3)
(int i=0;i-->
for(int i=0;i
1)
int lineTotal=0。。。字符文本[lineTotal][MAX_LINE_LENGTH]==>
字符文本[0][MAX_LINE_LENGTH]。在确定
lineTotal
后移动。2)
charn[1]={0};fgets(n,800,stdin)
800
的大小无效。它与实际大小不匹配(
1
)。3)
(int i=0;i-->
用于(int i=0;i
for(int i = 0; i < lineTotal; i++){}
    fgets(text[i], MAX_WORD_LENGTH, stdin);
}