Search for strings within a text file



	#include <stdlib.h>
	#include <stdio.h>
	#include <string.h>
				
	const char email_receive_set_output_high_string[] = {"set_output_high"};
	const char email_receive_set_output_low_string[] = {"set output low"};

					
	
	FILE *fp=fopen("/home/pi/myproject/somefile","r");
	char  tmp[1024] = {0x0};
	while(fp != NULL && fgets(tmp, sizeof(tmp), fp) != NULL)		//fgets gets one line at a time, up to the max size - not the next block of max size.  So ensure the size is >= to the max line length within the file to ensure complete lines will be checked.
	{
		if (strstr(tmp, email_receive_set_output_high_string))		//Do lines contain string 
		{
			//STRING FOUND
			printf("Setting output high\n");
			OUR_OUTPUT_PIN(1);
			
		}
		if (strstr(tmp, email_receive_set_output_low_string))
		{
			//STRING FOUND
			printf("Setting output low\n");
			OUR_OUTPUT_PIN(0);
		}
		
	}
	if(fp != NULL)
		fclose(fp);

 

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Jake

    5 years ago

    this does not compile:

    error: expected unqualified-id before ‘while’

    is the error message received when I try to compile with code::blocks on my Pi

Comments

Your email address will not be published.