簡體   English   中英

C-從文本文件打印特定行

[英]C - Print Specific Line From Text File

我是編程新手,所以請保持友好。

當前,我正在嘗試編寫一個程序來打開一個文本文件,讀取兩個單詞,搜索該文本文件以計算兩個單詞出現的次數,然后最終打印出第一個單詞出現的第一行。

到目前為止,這是我所做的:

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

FILE *infile;
char inputWord1[100], inputWord2[100], filename[100], wordInText[100], line[500]; 
int i, count, strComp, word1Count, word2Count, wordLen, lineCount; 
char c;

int main() { 
    printf("Enter the first word: "); 
    gets(inputWord1); 
    printf("Enter the second word: "); 
    gets(inputWord2); 
    printf("Enter the file name: "); 
    gets(filename); 

    infile = fopen(filename, "r"); 
    if(infile == NULL) { 
        printf("Error"); 
        exit(1); 
    } 

    word1Count = 0; word2Count = 0; lineCount = 1;
    while(fscanf(infile, "%s", wordInText) != EOF) { 
        wordLen = strlen(wordInText);
        for(i = 0; i < wordLen; i++) {
            if(wordInText[i] >= 65 && wordInText[i] <= 90) { 
                wordInText[i] = wordInText[i] + 32; 
            }
        }

        for(c = getc(infile); c != EOF; c = getc(infile)) {
            if(c == '\n') { 
                lineCount = lineCount + 1;
            }
        }

        strComp = strcmp(wordInText, inputWord1); 
        if(strComp == 0) { 
            word1Count++;
            if(word1Count == 1) { 
                for(int x = lineCount; x <= lineCount; x++) {
                    fgets(line, 500, infile); 
                    printf("%s\n", line);
                }
            }
        }
        strComp = strcmp(wordInText, inputWord2); 
        if(strComp == 0) { 
            word2Count++; 
        }
    }
    printf("Word 1 appears %d times\n", word1Count); 
    printf("Word 2 appears %d times\n", word2Count);
}

因此,所有這一切都有效,除了:

strComp = strcmp(wordInText, inputWord1); 
        if(strComp == 0) { 
            word1Count++;
            if(word1Count == 1) { 
                for(int x = lineCount; x <= lineCount; x++) {
                    fgets(line, 500, infile); 
                    printf("%s\n", line);
                }
            }
        }

最后一個for循環無法正常工作。 它打印出\\ n,但不打印該行。 我真的不知道為什么它不起作用。 所有其他部分都工作正常。

如果有人對如何解決這個問題有任何想法,我將不勝感激。 請記住,我只知道基本的C函數,並且我還沒有完全完成該程序(仍然需要將輸入的單詞轉換為小寫)。

應該替換gets以避免緩沖區溢出。我使用了一些define,當我們找到輸入行是dup並在最后打印時。 文件是逐行讀取的,每一行都是逐字分割的。 解析應進行更新,例如允許有多個空格,支持更多的單詞分隔符等。

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

#define WORD_LEN_MAX 100                                                       
#define LINE_LEN_MAX 500                                                       

int main() {                                                                   
    FILE *infile;                                                              
    char inputWord1[WORD_LEN_MAX], inputWord2[WORD_LEN_MAX];                   
    char filename[WORD_LEN_MAX];                                               
    char wordInText[WORD_LEN_MAX], line[LINE_LEN_MAX];                         
    char firsAppear[LINE_LEN_MAX];                                             
    int word1Count, word2Count, lineCount, i;                                  
    printf("Enter the first word: ");                                          
    gets(inputWord1);                                                          
    printf("Enter the second word: ");                                         
    gets(inputWord2);                                                          
    printf("Enter the file name: ");                                           
    gets(filename);                                                            


    infile = fopen(filename, "r");                                             
    if(infile == NULL) {                                                       
        printf("Error cannot open %s", filename);                              
        exit(1);                                                               
    }                                                                          

    word1Count = 0; word2Count = 0; lineCount = 1;                             
    while(fgets(line, sizeof(line), infile) != NULL) {                         
        char *p = line;                                                        
        lineCount++;                                                           
        while (*p != '\0' && *p != '\n') {                                     
            i = 0;                                                             
            while (*p != ' ' && *p != '\0' && *p != '\n') {                    
                wordInText[i++] = tolower(*p++);                               
            }                                                                  
            if (*p == ' ') {                                                   
                p++;                                                           
            }                                                                  
            wordInText[i] = '\0';                                              

            if(!strcmp(wordInText, inputWord1)) {                              
                word1Count++;                                                  
                if(word1Count == 1) {                                          
                    strncpy(firsAppear, line, sizeof(firsAppear));             

                }                                                              
            }                                                                  
            if(!strcmp(wordInText, inputWord2)) {                              
                word2Count++;                                                  
            }                                                                  
        }                                                                      
    }                                                                          
    printf("Word 1 appears %d times\n", word1Count);                           
    printf("Word 2 appears %d times\n", word2Count);                           
    printf("%s", firsAppear);                                                  
}

有很多方法可以做到這一點。 但是,您選擇的方法必須朝着更困難的方向發展。 在尋找正確的工具來使用時,在讀取線時 ,使用面向行的輸入幾乎總是正確的選擇。 因此, gets 永遠不是正確的選擇。 它非常不安全,已從C庫中刪除,請改用fgets (或getline )。

避免使用全局變量 在您的程序中沒有用。 它們的用途有限,但是對於簡單的程序,您幾乎永遠不會使用它們。

當您需要比較單詞並將匹配項的總數相加時,只需執行...比較,如果它們匹配,就可以增加總和。 沒有其他要求。

至於lineCount如果您使用的是面向行的輸入,這是微不足道的,只需為讀取的每一行增加計數器。

但是,如何使單詞測試呢? strtok (或strsep )是提供的工具。 只需閱讀一行並將其標記化即可 然后比較並遞增。

我舉了一個簡短的例子。 (大多數代碼只是從輸入末尾newline )。 這很簡單,讀取一行,標記化,比較並在匹配時增加,增加lineCount。 完成:

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

/* Avoid Globals */

#define MAXL 500
#define MAXW 100

int main (void)
{
    char inputWord1[MAXW] = { 0 };      /* Always Initialize your Variables */
    char inputWord2[MAXW] = { 0 };
    char filename[MAXW] = { 0 };
    char line[MAXL] = { 0 };
    char *token = NULL;
    char *delim = " ,.;\n";
    size_t word1Count = 0;              /* can't be negative, use   */
    size_t word2Count = 0;              /* size_t or unsigned       */
    size_t lineCount = 0;
    size_t len = 0;
    FILE *infile = NULL;

    printf ("\nEnter the first word: ");        /* Do NOT use gets, it is insecure  */
    fgets (inputWord1, MAXW, stdin);            /* use fgets or getline instead     */        

    len = strlen (inputWord1);
    while (len > 0 && (inputWord1[len-1] == '\n' || inputWord1[len-1] == '\r'))
        inputWord1[--len] = 0;                  /* strip newline or carriage return */

    printf ("\nEnter the second word: ");
    fgets (inputWord2, MAXW, stdin);

    len = strlen (inputWord2);
    while (len > 0 && (inputWord2[len-1] == '\n' || inputWord2[len-1] == '\r'))
        inputWord2[--len] = 0;                  /* strip newline or carriage return */

    printf ("\nEnter the file name: ");
    fgets (filename, MAXW, stdin);

    len = strlen (filename);
    while (len > 0 && (filename[len-1] == '\n' || filename[len-1] == '\r'))
        filename[--len] = 0;                    /* strip newline or carriage return */

    infile = fopen (filename, "r");
    if (infile == NULL) {
        printf ("error: file open failed. '%s'\n", filename);
        exit (1);
    }

    printf ("\nThe lines processed are:\n\n");

    /* read each line, tokenize, compare and increment */
    while (fgets (line, MAXL, infile) != NULL)
    {
        len = strlen (line);
        while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
            line[--len] = 0;                    /* strip newline or carriage return */

        printf ("  %2zu  %s\n", lineCount, line);
        for (token = strtok (line, delim); token != NULL; token = strtok (NULL, delim)) 
        {
            if (strcmp (token, inputWord1) == 0)
                word1Count++;
            if (strcmp (token, inputWord2) == 0)
                word2Count++;
        }

        lineCount++;
    }

    printf ("\nWord 1 appears %zu times\n", word1Count);
    printf ("Word 2 appears %zu times\n", word2Count);
    printf ("Number of lines: %zu\n\n", lineCount);

    return 0;
}

使用/輸出

$ ./bin/countw1w2

Enter the first word: me

Enter the second word: chequer

Enter the file name: dat/ll_replace_poem.txt

The lines processed are:

   0  Eye have a spelling chequer,
   1  It came with my Pea Sea.
   2  It plane lee marks four my revue,
   3  Miss Steaks I can knot sea.
   4  Eye strike the quays and type a whirred,
   5  And weight four it two say,
   6  Weather eye am write oar wrong,
   7  It tells me straight aweigh.
   8  Eye ran this poem threw it,
   9  Your shore real glad two no.
  10  Its vary polished in its weigh.
  11  My chequer tolled me sew.
  12  A chequer is a bless thing,
  13  It freeze yew lodes of thyme.
  14  It helps me right all stiles of righting,
  15  And aides me when eye rime.
  16  Each frays come posed up on my screen,
  17  Eye trussed too bee a joule.
  18  The chequer pours over every word,
  19  Two cheque sum spelling rule.

Word 1 appears 4 times
Word 2 appears 4 times
Number of lines: 20

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM