Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9131583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:09:52+00:00 2026-06-17T08:09:52+00:00

I am trying to read in a file, find the string myprop and after

  • 0

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);
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:09:53+00:00Added an answer on June 17, 2026 at 8:09 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find and read a string from a text file using a
I'm trying to find a way to read in a WSDL file (I will
I am trying to read a file and then display the file in ASCII
I'm trying to open .doc file and read its content. But i can't find
I'm trying to read a c-style unicode string from a memory-mapped file and IndexOutOfRangeException
I am trying to read file headers using java, I want to get file
I am trying to read file and split its line to get some context(Computer
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using
I'm trying to read a file line by line starting from a specific line
I am trying to read a file(temp.txt) placed under /root directory on remote host(ubuntu)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.