如何在特定的字符串后打印的值 [英] how to print a value after a specific string

查看:108
本文介绍了如何在特定的字符串后打印的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个文件阅读,查找字符串myprop和myprop将是一个=号,然后一个号码后显示。我需要打印出只是数字作为一个字符串,摆脱空格和评论。我能够找到myprop字符串,然后我相信我应该使用的fscanf,但我有麻烦。

 为const char * get_filename_property()
{
    为const char *文件名=myfile.properties;
    为const char * propkey =myprop;
    炭缓冲器[100],* buffPtr,lastChar,值[50];
    INT line_num = 1,i = 0;    FILE * FP;
    FP = FOPEN(myfile.properties,R);
    如果(FP == NULL)
        PERROR(打开文件时出错\\ n \\ n);    而(与fgets(缓冲器,100,FP)!= NULL)
    {
        如果((的strstr(缓冲液,propkey))!= NULL)
        {
            的printf(Myprop在线找到数:%d \\ n,l​​ine_num);
            的printf(\\ n%S \\ n,缓冲区);
        }
        line_num ++;
    }
    如果(FP)
        FCLOSE(FP);
}INT主(INT ARGC,CHAR *的argv [])
{
    get_filename_property();
    系统(暂停);
    返回(0);
}


解决方案

您可以添加的sscanf 的那一刻,当你找到 mypop 字符串的文件中。添加以下行while循环:

 的sscanf(buf中,%* [^ =] =%[^ \\ n],值);

%* [^ =]:这意味着,scanf函数capte所有字符以前生产 = 和忽略它

%[^ \\ n]:这意味着你在后 = 直到capting所有字符您的缓冲区字符串的末尾(甚至空格字符)。只有在字符串值的一开始就空格字符不会capted

添加这种方式

 而(与fgets(缓冲区,100,FP)!= NULL)
 {
  如果((的strstr(缓冲液,propkey))!= NULL)
  {
   的printf(Myprop在线找到数:%d \\ n,l​​ine_num);
   的printf(\\ n%S \\ n,缓冲区);
   sscanf的(BUF,%* [^ =] =%[^ \\ n],值);
   的printf(\\ n值为%s \\ n,值);
   打破;
  }
  line_num ++;
 }

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 of blank spaces and comments. I am able to locate the "myprop" string and then I believe I should be using fscanf but I am having trouble with that.

const char *get_filename_property()
{
    const char *filename  = "myfile.properties";
    const char *propkey = "myprop";
    char buffer[100], *buffPtr, lastChar, value[50];
    int line_num=1, i=0;

    FILE *fp;
    fp=fopen("myfile.properties", "r");
    if (fp == NULL)
        perror("Error opening file\n\n");

    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);
        }
        line_num++;
    }
    if (fp)
        fclose(fp);
}  

int main(int argc, char *argv[])
{
    get_filename_property();
    system("pause");
    return(0);
}    

解决方案

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++;
 }

这篇关于如何在特定的字符串后打印的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆