如何在文件中搜索与给定字符串匹配的字符串?

how to search around a file for a matching string with a given one?

void merr_liber()
{
  FILE *fp_merr_liber;
  FILE *fp_librat;
  
  struct merr_liber input;
  struct librat liber;
  
  char isbn[13];
 
  int zgjedhja;
  
  fp_merr_liber = fopen("librat_e_marre.txt", "ab+");
  fp_librat = fopen("librat.txt", "r");
  

  if (fp_merr_liber == NULL)
  {
    printf("\nError merr_librat.txt nuk mund te hapet\n\n");
    exit(1);
  }
  if (fp_librat == NULL)
  {
    printf("\nError librat.txt nuk mund te hapet\n\n");
    exit(1);
  }
  while (!feof(fp_librat))
  {
     printf("\nISBN: ");
     scanf("%s", isbn);
     fscanf(fp_librat, "%s", liber.isbn);
     if (unike(isbn, 13) && valide(isbn, 13) && !strcmp(isbn, liber.isbn))
     {
        strcpy(input.isbn, isbn);
        fprintf(fp_merr_liber, "%s\n", input.isbn);
        break;
     }
     if (strcmp(isbn, liber.isbn) != 0)
     {
        printf("Nuk ekziston nje liber me kete isbn.\n");
        printf("Deshironi te vazhdoni ? (1- vazhdo 0- kthehu ne menune kryesore): ");
        scanf("%d", &zgjedhja);
        
        if (zgjedhja == 1)
        {
            continue;
        }
        else
        {
            menu();
        }
     }
     printf("Gabim ne formatin e isbn, isbn ka 13 karaktere, te cilat jane unike.");
  }

  
  fclose(fp_merr_liber);
  fclose(fp_librat);
}

我有一个函数可以从用户那里获取字符串 isbn,然后将其与保存在文件中的结构 librat 的成员进行比较。我遇到了困难,因为我的 while 循环只搜索第一行而不是整个文件。 内部函数仅用于格式化,对写入或读取文件没有任何作用。 标签是用我的母语写的,我希望除此之外代码仍然可读。 我只需要一个实例,因为字符串 isbn 是唯一的

您试图在 while 循环的每次迭代中获取一个新字符串作为输入,这可能不是您想要的。

你可以做的一件事是创建一个不同的 while 循环并使用 fscanf() 直到到达文件末尾并将从文件中获取的每个字符串与你想要查找的字符串进行比较,也不要忘记在到达文件末尾后将文件位置指针移动到文件的开头。

"I only need one instance since the string isbn is unique"

如前所述,while(!feof(fp)) is always wrong

有很多方法可以做到这一点,一种替代方法是使用 while(fgets(...)) {...} line by line with strstr().

下图不是完整的解决方案,但希望能为您提供一个起点,您可以在此基础上进行扩展以满足您的需求:

char *tok = NULL;
int len = strlen(fp_librat);
char line[MAX_LEN] = {0};//#define this elsewhere
while(fgets(line, sizeof line, fp_librat))
{
     tok = strstr(line, input.isbn)// look for search word in each line of file.
     if(tok)//captured pointer to trailing content starting at searched word
     {
             strcpy(line, tok); //places content starting at found word into buffer           
             if((line[len] == ' ')||//qualify what you have found by 
             (line[len] == '\n')||  //looking for characters that can
             (line[len] == '\t')||  //follow a string...
             (line[len] == '.')||
             (line[len] == ','))
             //there are others, but you get the idea
             {
                  line[len] = 0;//null terminate
                  //at this point the search is done.  You have confirmed that the search word
                  //is in the content searched.  
                  line[strcspn(line, " ")] = 0;// optional extract word from rest of content.
                  break; //not optional: search is finished, leave and report 'found'
             }
             else //keep looking, (found a substring, not the search string.)
     }
}
           
     break;