使用printf在C中打印多个字符串

时间:2013-01-16 20:16:51

标签: c string printf scanf

  

可能重复:
  how to print a value after a specific string

因此,对于我的原始作业,我必须在字符串“myprop =”之后打印单个字符,但不是我被告知我需要在=符号后打印几个字符串而不打印注释。

一个例子是:myprop =这是一个句子//值。基本上我需要修改sscanf和printf函数。

const char *get_filename_property()
{
  const char *filename  = "myfile.properties";
  const char *propkey = "myprop=";
  char buffer[1024], *buffPtr, lastChar, value[50];
  int line_num=1;
  FILE *fp;
  fp=fopen("myfile.properties", "r");
  if (fp == NULL)
  {  
    perror("Error opening file\n\n");
    return -1;
  } 

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

  if(fp)
  { 
    fclose(fp); //close the file if it is still open
  }

  return 0;
}      

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

0 个答案:

没有答案