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

 

 

 

Comments

  1. Jake

    9 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