阅读背景:

打印从包含特定单词的文件中读取的行

来源:互联网 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()

{
  int i, p=0;;
    int c;
    char file_name[100];
    char  search[10];

    printf("Enter the file name:");
    scanf("%s", file_name);
    printf("Search word:");
    scanf("%s", search);

    FILE *f = fopen((strcat(file_name, ".txt")), "rb");
    fseek(f, 0, SEEK_END);
    long pos = ftell(f); 
    fseek(f, 0, SEEK_SET);

    char *bytes = malloc(pos + 1);
    fread(bytes, pos, 1, f);
    bytes[ pos ] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()

{
  int i, p=0;;
    int c;
    char file_name[100];
    char  search[10];

    printf("Enter the file name:");
    scanf("%s", file_name);
    printf("Search word:");
    scanf("%s", search);

    FILE *f = fopen((strcat(file_name, ".txt")), "rb");
    fseek(f, 0, SEEK_END);
    long pos = ftell(f); 
    fseek(f, 0, SEEK_SET);

    char *bytes = malloc(pos + 1);
    fread(bytes, pos, 1, f);
    bytes[ pos ] = '\0'; 

/*search*/

    if (strstr(bytes, search) != NULL){
        printf("found\n");
        p = 1;}
    else{
        printf("Not found\n");

        p=0;}

    free(bytes);

   char *found = strstr( bytes, search );
   if ( found != NULL )
   {
    char *lineStart;
    for(lineStart = strchr(bytes, '\n');  !strcmp(lineStart,"\n"); 
        lineStart = strchr(lineStart+1, '\n')){
      printf("%s\n", lineStart);  
   }
  }
}

The above stated code is supposed to search for a word in a file (.txt) and if found it should print "found" and print the line it was found in. For example, if searched for a word "Brick" in the file, and if found in a sentence like "The house is made of red bricks", then it prints the whole sentence as the output i.e "The house is made of the red bricks".

上面提到的代码应该在文件(.txt)中搜索一个单词,如果找到它应该打印“找到”并打印它找到的行。例如,如果在文件中搜索单词“Brick”如果在“房子是用红砖砌成的”这样的句子中找到,那么它打印整个句子作为输出,即“房子是由红砖砌成的”。

I am having trouble printing the line containing the search word. I am trying to use pointer to move to the start of the current line and then navigate incrementally but I am kinda stuck in how to make the pointer stop at the end of the line and just print up till that point.

我在打印包含搜索词的行时遇到问题。我试图使用指针移动到当前行的开头,然后逐步导航,但我有点陷入如何使指针停在行的末尾,只是打印到那一点。

1 个解决方案

#1


The issue with your code is, you call free(bytes); inside your code and after that, you continue to use bytes. This invokes undefined behavior.

您的代码的问题是,您调用free(bytes);在你的代码中,然后继续使用字节。这会调用未定义的行为。

Also, i'll suggest

另外,我建议

  1. change your scanf() instruction

    更改scanf()指令

    scanf("%s", file_name);
    

    and

    scanf("%s", search);
    

    to

    scanf("99%s", file_name);
    

    and

    scanf("9%s", search);
    

    to avoid the risk of buffer overflow.

    避免缓冲区溢出的风险。

  2. Always check for the success of fopen() before using the returned pointer.

    在使用返回的指针之前,请务必检查fopen()是否成功。

However, from the logical point, i'll suggest you

但是,从逻辑上看,我会建议你

  1. Read a whole line by line from the file using fgets()
  2. 使用fgets()从文件中逐行读取一行

  3. Search for the particular word using strstr().
  4. 使用strstr()搜索特定单词。

  5. If found, print the whole line, otherwise, continue to step 1 until fgets() return NULL.
  6. 如果找到,则打印整行,否则,继续执行步骤1,直到fgets()返回NULL。

Notes:

  1. the recommended signature of main() is int main(void).
  2. main()的推荐签名是int main(void)。

  3. Always initialize all local variables.
  4. 始终初始化所有局部变量。


'; /*search*/ if (strstr(bytes, search) != NULL){ printf("found\n"); p = 1;} else{ printf("Not found\n"); p=0;} free(bytes); char *found = strstr( bytes, search ); if ( found != NULL ) { char *lineStart; for(lineStart = strchr(bytes, '\n'); !strcmp(lineStart,"\n"); lineStart = strchr(lineStart+1, '\n')){ printf("%s\n", lineStart); } } } #include <stdio.h> #include <stdlib.h> #include



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: