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
sscanfin the moment when you find themypopstring in the file. Add the following line in your while loop:"%*[^=]": 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 captedadd it in this way