how to print a value after a specific string

前端 未结 1 664
眼角桃花
眼角桃花 2021-01-27 08:37

I am trying to read in a file, find the string \"myprop\" and after \"myprop\" will be an \"=\" sign then a number. I need to print out just that number as a string, getting rid

1条回答
  •  醉梦人生
    2021-01-27 09:26

    You can add sscanf in the moment when you find the mypop string in the file. Add the following line in your while loop:

    sscanf(buf,"%*[^=]= %[^\n]",value);
    

    "%*[^=]": This means that scanf capte all characters befor the = and ignore it

    " %[^\n]": This means that you are capting all characters after the = till the end of your buffer string (even the space characters). only the space characters in the beggining of the value string will not capted

    add it in this way

    while(fgets(buffer, 100, fp) != NULL)
     {
      if((strstr(buffer, propkey)) != NULL)
      {
       printf("Myprop found on line: %d\n", line_num);
       printf("\n%s\n", buffer);
       sscanf(buf,"%*[^=]= %[^\n]",value);
       printf("\nvalue is %s\n", value);
       break;
      }
      line_num++;
     }
    

    0 讨论(0)
提交回复
热议问题