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;
}