Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-打印文本文件中的特定行_C_File_Search - Fatal编程技术网

C-打印文本文件中的特定行

C-打印文本文件中的特定行,c,file,search,C,File,Search,我是编程新手,所以请友好一点 目前我正试图编写一个程序,打开一个文本文件,读入两个单词,在文本文件中搜索,计算两个单词出现的次数,然后最终打印第一个单词出现的第一行 到目前为止,我所做的就是: #include <stdio.h> #include <string.h> #include <stdlib.h> FILE *infile; char inputWord1[100], inputWord2[100], filename[100], word

我是编程新手,所以请友好一点

目前我正试图编写一个程序,打开一个文本文件,读入两个单词,在文本文件中搜索,计算两个单词出现的次数,然后最终打印第一个单词出现的第一行

到目前为止,我所做的就是:

#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);
}
#包括
#包括
#包括
文件*填充;
字符inputWord1[100],inputWord2[100],文件名[100],wordInText[100],行[500];
int i,count,strComp,word1 count,word2 count,wordLen,lineCount;
字符c;
int main(){
printf(“输入第一个单词:”);
获取(inputWord1);
printf(“输入第二个单词:”);
获取(inputWord2);
printf(“输入文件名:”);
获取(文件名);
infle=fopen(文件名为“r”);
如果(infle==NULL){
printf(“错误”);
出口(1);
} 
word1Count=0;word2Count=0;lineCount=1;
而(fscanf(infle,“%s”,wordInText)!=EOF){
wordLen=strlen(wordInText);
对于(i=0;iif(wordInText[i]>=65&&wordInText[i]应该替换get以避免缓冲区溢出
我使用了一些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);                                                  
}
#包括
#包括
#包括
#包括
#定义单词_LEN_MAX 100
#定义线长度最大值500
int main(){
文件*填充;
char inputWord1[WORD_LEN_MAX],inputWord2[WORD_LEN_MAX];
字符文件名[WORD_LEN_MAX];
char wordInText[WORD_LEN_MAX],line[line_LEN_MAX];
char firsAppear[行长度最大];
int-word1Count、word2Count、lineCount、i;
printf(“输入第一个单词:”);
获取(inputWord1);
printf(“输入第二个单词:”);
获取(inputWord2);
printf(“输入文件名:”);
获取(文件名);
infle=fopen(文件名为“r”);
如果(infle==NULL){
printf(“错误无法打开%s”,文件名);
出口(1);
}                                                                          
word1Count=0;word2Count=0;lineCount=1;
while(fgets(line,sizeof(line),infle)!=NULL){
char*p=行;
lineCount++;
而(*p!='\0'&&&*p!='\n'){
i=0;
而(*p!='&&*p!='\0'&&&*p!='\n'){
wordInText[i++]=tolower(*p++);
}                                                                  
如果(*p=''){
p++;
}                                                                  
wordInText[i]='\0';
如果(!strcmp(wordInText,inputWord1)){
word1Count++;
如果(word1Count==1){
strncpy(firsAppear、line、sizeof(firsAppear));
}                                                              
}                                                                  
如果(!strcmp(wordInText,inputWord2)){
word2Count++;
}                                                                  
}                                                                      
}                                                                          
printf(“单词1出现%d次\n”,单词1计数);
printf(“Word 2出现%d次\n”,Word 2计数);
printf(“%s”,firsAppear);
}

有很多方法可以做到这一点。但是,你选择的方法必须朝着更困难的方向发展。在寻找正确的工具时,在阅读行时,使用面向行的输入几乎总是正确的选择。因此,
得到的
从不fgets
(或
getline

避免使用
#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);                                                  
}
#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