C-从文件中读取带引号的特定字符串

C-从文件中读取带引号的特定字符串,c,file,C,File,我正在尝试从文件中读取字符串。文件包含以下文本: <1a>This is line 1<1a> <2f>This is line 2<2f> <3c>This is line 3<3c> 在我的程序中,我得到这个值1a或2f。基于此,我需要提取特定的行,如2f,我只需要读取这是第2行,并将其保存在缓冲区中 我已经能用fopen fput读写了,但不知道怎么读。有人能给我指出一些正确的方向来说明如何读这篇文章吗。任何演示代码

我正在尝试从文件中读取字符串。文件包含以下文本:

<1a>This is line 1<1a>
<2f>This is line 2<2f>
<3c>This is line 3<3c>
在我的程序中,我得到这个值1a或2f。基于此,我需要提取特定的行,如2f,我只需要读取这是第2行,并将其保存在缓冲区中

我已经能用fopen fput读写了,但不知道怎么读。有人能给我指出一些正确的方向来说明如何读这篇文章吗。任何演示代码。谢谢

char* returnSpecificString(char* valueBetweenQuotes)
{
  FILE* file= fopen("test.txt", "r"); // Open in reading only mode
  char *line= malloc(sizeof(char[150]));
  if (NULL == line) // Checks if enough memory
    fprintf(stderr, "Not enough memory. \n");
  else
  {
    while(fgets(line, sizeof(line), file)) //Iterate until end of file
    {
       if (strstr(line, valueBetweenQuotes) != NULL) // Meaning it's the line we want
       return functionToExtractTextBetweenQuote();
    }
  }
  return line;
}

至于函数toextractTextBetweenNote,我建议您使用类似strtok strchror sprintf的函数,它将帮助您从字符串中提取所需内容。我知道这是不够的,但我现在没有时间完成它,所以我希望它能帮助你。

这应该可以做到:

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

int extract_line (char* line, char* buffer, char* ctag)
{
  char line_buffer[255] = {0};
  char* tagStart;
  char* tagEnd;

  if( strlen(line) < (sizeof(line_buffer)/sizeof(char)) )
  {
     strcpy(line_buffer,line);
  }
  else
  {
     printf("Line size is too big.\n");
     return 1;
  }

  tagStart = strstr(line_buffer,ctag);
  if(tagStart != NULL)
  {
    tagEnd = strstr(tagStart+1,ctag);
    if(tagEnd != NULL && (tagEnd > (tagStart + strlen(ctag))))
    {
        *(tagEnd) = '\0';
        strcpy(buffer, tagStart + strlen(ctag));
        printf("%s\n",buffer);
    }
    else
    {
      printf("Could not find closing tag.\n");
      return 1; 
    }
  }
  return 0;
}


int main ()
{
  char buffer[255] = {0};
  char line_buffer[255] = {0};
  char tag[] = "<2a>";

  char* cptr;
  FILE* data;
  data = fopen ("file.txt", "r");
  if (data == NULL)
  {
       printf("\n Failed to open file!");
  }
  else {
     while(( fgets( line_buffer, 255, data )) != NULL)
     {        
        cptr = strstr(line_buffer,tag);
        if(cptr == NULL)
        {
            continue;
        }
        if(!extract_line(line_buffer,buffer,tag))
        {
           //Do the rest of processing
           puts(buffer);
           strcpy(buffer,"");
        }
     }
     fclose (data);
  }
  return 0;
}

基本上,您需要做的是获取tag字段,并将其用作删除令牌的delimeter。只需获取行标记,然后使用它来提取数据。

您可以使用fgets从打开的文件中读取行。如果您能向我们展示您迄今为止所写的内容以及相应的输出,我们将很容易为您提供帮助。如果我理解的很好,你想输入一个函数1a,让它返回引号之间的对应行?a fgets,然后sscanf,并用strcmp检查它。b fgets然后通过memcpy和strstr提取。c、 几乎可以保证,你必须从文件的开头开始依次阅读每一行,直到找到你需要的那一行。然后,您可以缓存文件中该行的位置,以防再次需要它,但前提是您可能再次需要它。除非您创建一个索引,并且“缓存位置”是创建索引的变体,否则您将无法通过许多其他机制在文件中找到正确的行。您可以将整个文件映射到内存中,然后按照这种方式搜索,如果您愿意的话。看起来非常类似于XML。如果您可以将文件格式更改为真正的XML格式,那么您可以使用大量XML库中的一个…strchr->strstr?这段代码作为伪代码也是不够的。这两种代码都可能有用,这取决于她是如何做到的。我认为当你知道你在寻找什么字符串时,strstr是有用的,这里可能不是这样。首先请注意参数的类型是不同的。谢谢你的评论,@BLUEPIXY。我正在整块修复代码,在cpp.sh中测试不是最方便的。再次修改解决方案以解决新突出的问题。再次感谢@BLUEPIXY的评论!这是OP想要的行为吗?。。。我真傻,根本没看到。1 cptr=行缓冲区,tag;如果cptr==NULL,则这没有任何意义。2调用方需要事先清除缓冲区,并且需要检查是否设置了执行后值。