Thread: Code...
View Single Post
Old 11-05-2005, 11:36 PM   #3 (permalink)
LoneWolf071
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 834
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
Code:
/* 
Created By DarkRedRose( @)--}--- )
Created On November 05, 2005
Created For:Searching Documents, Just Enter The Document Name And 
This Will Locate It And Tell You Where.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <unistd.h> //use if in linux
#include <math.h>

#define s_buffer 80


char *flush(char *); 
/*
fgets leaves a pesky \n at the end of ever string to know when it's finished
so we make a function to get rid of it, hence the flush
*/


int main(int argc, char *argv[])
{
	char 
	string_to_lookfor[s_buffer], 
	string_searching[s_buffer],
	filename[s_buffer],
	temp_string[s_buffer];

	long unsigned int 
	search_number=0,
	for_i,
	for_j, //special variables for the for loop
	for_k;
	
	
	FILE *search_file;

	printf("What File Would You Like To Search?\n:"); //Asks Filename
	fgets(filename, s_buffer, stdin); //Takes filename
	
	if(strlen(filename) == 0 || strcmp(filename, "\n") == 0)
	{
		printf("Woops! You Forgot To Enter A Value, Quiting Now!\n");
		exit(0);
	}
		
	strcpy(filename, flush(filename));
	search_file = fopen(filename, "r"); //opens the filename and assigns the stream to the searchfile pointer

	if(search_file == NULL)
	{
		perror(search_file); 
		/*
		prints reason error, compiler will give you flack about it not 
		being a pointer, don't worry about that
		*/	
		exit(0); //quits if it doesn't exist
	}
	
	printf("\nWhat String Would You Like To Search For?\n:");
 	fgets(string_to_lookfor, s_buffer, stdin); 
 	
 	if(strlen(string_to_lookfor) == 0 || strcmp(string_to_lookfor, "\n") == 0)
	{
		printf("Woops! You Forgot To Enter A Value, Quiting Now!\n");
		exit(0);
	}
	
	strcpy(string_to_lookfor, flush(string_to_lookfor)); //takes off the end \n
	search_number = strlen(string_to_lookfor); //number of char's in the string
		
	for(for_i=0; !feof(search_file); for_i++)
	{
		fgets(string_searching, s_buffer, search_file);
		
		if(strlen(string_searching) == 0 || strcmp(string_searching, "\n") == 0)
		{
			continue;
		}
		
		strcpy(string_searching, flush(string_searching));
		
		for(for_j = 0; for_j < strlen(string_searching); for_j++)
		{
			for(for_k = 0; for_k < search_number; for_k++)
			{
				temp_string[for_k] = string_searching[for_k+for_j];
			}
			
			temp_string[for_k] = '\0';
			/*
			Ok, So temp_string likes to tag on ugly letters at the end, 
			so we need this go get rid of it
			*/
				
			if((strcmp(string_to_lookfor, temp_string)) == 0)
			{
				printf("Match Found For %s! It's On Line:%lu At Character:%lu\n",string_to_lookfor, for_i+1, for_j+1);
			}
		}
	}
	fclose(search_file);
	return 0;
}

char *flush(char *string_flush)
{
	if(string_flush[strlen(string_flush) - 1] == '\n')
	{
		string_flush[strlen(string_flush) - 1] = '\0'; //last char will be \0
	}
	return string_flush;
}
I got bored last night and created this searching algorithm, it works quite well, you can mod the output and all... i love it...
LoneWolf071 is offline   Reply With Quote