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 8051757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:22:22+00:00 2026-06-05T07:22:22+00:00

How can I scan a file for a word and then print the line

  • 0

How can I scan a file for a word and then print the line containing that word in C programming?

I am trying to find a way to scan a file for a keyword and then print only the line containing that word. I am off to a very rough start. Here is what I have:

#include <stdio.h>

void getDsk (void);

void getDsk ()
{
FILE* fp;
char result [1000];
fp = popen("diskutil list","r");
fread(result,1,sizeof(result),fp);
fclose (fp);

while(!feof(fp)) {
        if(//line contains "Test".)
        {
            //show line.
        }
    }
}

int main(int argc, const char * argv[])
{
    getDsk();
    return 0;
}

EDIT: This did what I needed.

#include <stdio.h>

void getDsk (void);

void getDsk ()
{
    printf("Your available Windows installations are:\n");
    FILE* fp = popen("diskutil list","r");
    char line[1024];
    while(fgets(line, sizeof(line), fp)) 
    {
        if (strstr(line,"Microsoft")) 
        {
            printf("%s", line);
        }
        // if line contains the text, print it
    }
}

int main(int argc, const char * argv[])
{
    getDsk();
    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-05T07:22:24+00:00Added an answer on June 5, 2026 at 7:22 am

    A few things:

    1) You should pretty much never write while(!feof(fp)) { ... } It is a broken pattern because feof will only return true after a read attempt. So you are open to off by one errors.

    2) why the heck are you doing this?

    fp = popen("diskutil list","r");
    fread(result,1,sizeof(result),fp);
    fclose (fp);
    

    You are reading 1000 chars of the file then closing it, then later your stub code acts as if it plans on reading the file some more… makes no sense.

    3) here’s a reasonable approach:

    char line[1024];
    while(fgets(line, sizeof(line), fp)) {
        // if line contains the text, print it
    }
    

    what is going on here is fgets will read a line and return non-NULL if it succeeds. (NULL is treated as “false”, non-NULL is “true” when used in a test).

    So it is saying “while we could successfully read a line, do the following”, which is really what you want. Also, I am making the assumption that no lines will exceed 1024 characters which is usually a reasonable assumption, if this is not the case, then you will need to adjust accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I scan through a file which contains email addresses that are separated
I have a 200,000 line file that I'm trying to import, but I get
Is there a tool available that can scan a C# or VB.NET project and
I've created an iPhone app that can scan an image of a page of
I'm interested in developing an app for iPhone that can scan and read ITF
Is there any magic tools i can use to scan source code that was
High Level With StructureMap, Can I define a assembly scan rule that for an
it's can be apply not only to wordpress. But to all blog platform that
Quick question guys, I am trying to configure a virus scanner to scan file
I have a text file that I am reading in python . I'm trying

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.