可能重复:
如何在特定字符串之后打印值
因此,对于我的原始作业,我必须在字符串“myprop=”之后打印一个字符,但不是有人告诉我,我需要在 = 符号之后打印几个字符串而不打印注释。
一个例子是:myprop=this is a sentence //value。基本上我需要修改 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);
}