Help with searching text file

This is a discussion on Help with searching text file within the C Programming forums, part of the General Programming Boards category; Hi all, I've gotten very close with my project and just need a fresh pair of eyes to point out ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    26

    Help with searching text file

    Hi all, I've gotten very close with my project and just need a fresh pair of eyes to point out the obvious. My code seems to parse through the text file, yet does not actually search correctly. My search term of 'the' gave back 0 results, even though that word is obviously in there.

    Code:
    /*
     ask the user for a word
     convert user word to LOWER CASE
     open output file
     open input file
     test to be sure input file is open
     search for target word and keep count --> how??
     print results to monitor
     write results to file
     close files
     */
    #include<stdio.h>
    #include<stdlib.h>
    
    int main (void)
    {
    	//declare 
    	int i =0;
    	int count = 0;
    	
    	/*************************************************************
    	 working with arrays and strings
    	*************************************************************/
    	char mystring[50]; //what user puts in
    	char target[50]; //the word in the file we are looking for
    	
    	printf("input your message ");
    	scanf("%s", mystring);
    	//printf("%s", mystring);
    	
    	
    	/*************************************************************
    	 find file, write to it, output the string, end and close file
    	**************************************************************/
    	
    	//define text file to use
    	FILE * cfile;	
    	//name of file == file
    	cfile = fopen("./thanksgiving_proclamation.txt", "a+");
    	
    	//error handling if file does not exist
    	if(cfile == NULL) printf("Cannot open file");
    	
    	/*************************************************************
    	         parse through file and search for string
    	**************************************************************/	
    	//convert string to lowercase
    	for(i = 0; i < /*strlen(mystring)*/ 500; i++)//convert to string length
    	{
    		if(target[i] >= 'A' && target[i] <='Z')
    			//convert char between a and z into lowercase
    			target[i] = target[i] + 32; //makes uppercase char
    	}
    	
    	//compare our strings
    	do{
    		//scan through file
    		fscanf(cfile, "%s", mystring);	
    		
    		//convert string to lowercase
    		for(i = 0; i < /*strlen(mystring)*/ 300; i++)//convert to string length
    		{
    			if(mystring[i] >= 'A' && mystring[i] <='Z')
    				//convert char between a and z into lowercase
    				mystring[i] = mystring[i] + 32; //makes uppercase char
    		}
    		if(strcmp(mystring, target) == 0)
    			count++;
    	}while(!feof(cfile));
    	
    	//while(strcmp(target,"quit")!=0)//end loop
    	
    	   
    	//print to file
    	fprintf(cfile, "\nYour search '%s' was found %d times\n", mystring, count);
    	   
    	//close file
    	fclose(cfile);
    	
    	//show user file has been written
    	printf("\nSuccess. File has been written\n");
    	
    	printf("Your search '%s' was found %d times \n", mystring, count);
    	
    	printf("Press Enter to Continue...");
    	getchar();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at the string right in this part of the code:

    Code:
    	//compare our strings
    	do{
    		//scan through file
    		fscanf(cfile, "%s", mystring);	
    		
    		//convert string to lowercase
    		for(i = 0; i < /*strlen(mystring)*/ 300; i++)//convert to string length
    		{
    			if(mystring[i] >= 'A' && mystring[i] <='Z')
    				//convert char between a and z into lowercase
    				mystring[i] = mystring[i] + 32; //makes uppercase char
    		}
    
                    printf("\n mystring: %s \n   target:%s mystring, target);
                    //note particularly where the cursor is positioned after it prints the last word, 
                    //and any double spacing (vertically), which indicates a trailing newline is
                    //attached to the end of the word. Usually it's a trailing newline or EOS char
                    //difference in the words that goofs up the comparison.
    
                    j = getchar(); //add int j to your variables
    
    		if(strcmp(mystring, target) == 0)
    			count++;

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    26
    Hmm. Here is what I added:

    Code:
    //compare our strings
    	do{
    		//scan through file
    		fscanf(cfile, "%s", mystring);	
    		
    		//convert string to lowercase
    		for(i = 0; i < /*strlen(mystring)*/ 300; i++)//convert to string length
    		{
    			if(mystring[i] >= 'A' && mystring[i] <='Z')
    				//convert char between a and z into lowercase
    				mystring[i] = mystring[i] + 32; //makes uppercase char
    		}
    		printf("\n mystring: %s \n target:%s", mystring, target);
    		int j;
    		j = getchar(); //add int j to your variables
    		
    		if(strcmp(mystring, target) == 0)
    			count++;
    	}while(!feof(cfile));
    And here is what I'm seeing:

    input your message the

    mystring: the
    target:
    Success. File has been written
    Your search 'the' was found 0 times
    Press Enter to Continue...

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well I am not sure of your intent regarding the code for your target array. That seems to be pointless here. Also your are scaning your file for "mystring" then afterwards converting that to lower case?

    When you actually compare mystring to target, what is target here, what it initialized to be?

    Not sure, but is your intent to store the fscan function into target instead of mystring?


    Also, not good practice to imbed /*comments */ within the statement. Please include comments at the head of the block/test/code piece.
    Last edited by slingerland3g; 11-30-2009 at 04:22 PM. Reason: Clarification for target or mystring assingment...

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    26
    My goal is to compare the user input (mystring) with the content of the text file (target). If they match then I need to keep a record (using my count variable) and print out how many times the user input (mystring) is seen in the text file.

    I'm lost at the point of comparing

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    In a quick test, if you simply alter your code to use 'target' instead of mystring argument you should fare much better!

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by slingerland3g View Post
    In a quick test, if you simply alter your code to use 'target' instead of mystring argument you should fare much better!
    Sorry, I am not exactly sure where I should switch mystring for target regarding your reply.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good catch, Slingerland!

    Zachary, you're bringing in the file words, and putting them into the wrong string.

    Put them into the "target" string, instead.

    Code:
    //scan through file
    		fscanf(cfile, "%s", mystring);	//change this to target
    

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    One more thing, there is some stack smashing happening as well, that I see. You may want to alter your loops to go as far as the string length, as you commented out. You will need to include string.h for this. Move your loop too alter your "target" string inside your 'do-while'.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by slingerland3g View Post
    One more thing, there is some stack smashing happening as well, that I see. You may want to alter your loops to go as far as the string length, as you commented out. You will need to include string.h for this. Move your loop too alter your "target" string inside your 'do-while'.
    Ah, the missing #include<string.h> is what was making the string length not work. Thanks for that

    Also thanks Adak for pointing out the wrong string I'm looking for I really appreciate the help, as my eyes are bleeding from looking over this beast.

    While I'm making progress I still can't get it to actually register with the textfile (target array) and match up my user input (mystring array). Below is revised code up to this point,

    Code:
    /*
     ask the user for a word
     convert user word to LOWER CASE
     open output file
     open input file
     test to be sure input file is open
     search for target word and keep count --> how??
     print results to monitor
     write results to file
     close files
     */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main (void)
    {
    	//declare 
    	int i =0;
    	int count = 0;
    	
    	/*************************************************************
    	 working with arrays and strings
    	*************************************************************/
    	char mystring[50]; //what user puts in
    	char target[50]; //the word in the file we are looking for
    	
    	printf("input your message ");
    	scanf("%s", mystring);
    	//printf("%s", mystring);
    	
    	
    	/*************************************************************
    	 find file, write to it, output the string, end and close file
    	**************************************************************/
    	
    	//define text file to use
    	FILE * cfile;	
    	//name of file == file
    	cfile = fopen("./thanksgiving_proclamation.txt", "a+");
    	
    	//error handling if file does not exist
    	if(cfile == NULL) printf("Cannot open file");
    	
    	/*************************************************************
    	         parse through file and search for string
    	**************************************************************/	
    	//convert string to lowercase
    	for(i = 0; i < strlen(mystring); i++)//convert to string length
    	{
    		if(target[i] >= 'A' && target[i] <='Z')
    			//convert char between a and z into lowercase
    			target[i] = target[i] + 32; //makes uppercase char
    	}
    	
    	//compare our strings
    	do{
    		//scan through file
    		fscanf(cfile, "%s", target);	
    		
    		//convert string to lowercase
    		for(i = 0; i < strlen(mystring); i++)//convert to string length
    		{
    			if(mystring[i] >= 'A' && mystring[i] <='Z')
    				//convert char between a and z into lowercase
    				mystring[i] = mystring[i] + 32; //makes uppercase char
    		}
    		/**********
    		 code help by adak on cboard
    		**********/
    		
    		printf("\n mystring: %s \n target:%s", mystring, target);
    		int j;
    		j = getchar(); //add int j to your variables
    		
    		/*********
    		 end code help
    		**********/
    		if(strcmp(mystring, target) == 0)
    			count++;
    	}while(!feof(cfile));
    	
    	//while(strcmp(target,"quit")!=0)//end loop
    	
    	   
    	//print to file
    	fprintf(cfile, "\nYour search '%s' was found %d times\n", mystring, count);
    	   
    	//close file
    	fclose(cfile);
    	
    	//show user file has been written
    	printf("\nSuccess. File has been written\n");
    	
    	printf("Your search '%s' was found %d times \n", mystring, count);
    	
    	printf("Press Enter to Continue...");
    	getchar();
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    26
    Would the issue of not comparing be in this part?

    Code:
    if(strcmp(mystring, target) == 0)
    			count++;

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    while(!feof(cfile)
    do not use feof to control loop - read FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why are you opening the text file, in append+ mode?

    Aren't you just reading the file, from first word to last word?

    This is what I think you were setting out to do with your program.


    Code:
    #include <stdio.h>
    #include <string.h>  //strlen()
    #include <ctype.h>   //isalpha()
    
    int main (void)
    {
    	//declare 
      FILE * cfile;	
      char ch;
      int len, j, i = 0;
      int count = 0;
    	
      char target[20]; //the word we are looking for
      char mystring[20] = { '\0' }; //the word from the file
    
    	/*************************************************************
    	 working with arrays and strings
    	*************************************************************/
    
       printf("\n\n\n\n"); //clean some space
    
    	/*************************************************************
    	 find file, write to it, output the string, end and close file
    	**************************************************************/
    	
    	//define and open text file
      cfile = fopen("blessing.txt", "rt");
    	
    	//error handling if file does not exist
      if(cfile == NULL) printf("Cannot open file");
    	
                // input target
      printf("Enter the target word I'm looking for: ");
      fgets(target, sizeof(target), stdin);
      printf("\nTarget: %s", target);
                //remove the newline fgets() adds, replace it with EOS char
      target[strlen(target) - 1] = '\0'; 
                
      
      for(i = 0; i < strlen(target); i++) //convert target to all lowercase
      {
         if(target[i] >= 'A' && target[i] <='Z')
    	target[i] = target[i] + 32; 
      }
      printf("\ntarget: %s", target);
    
    	/*************************************************************
    	         parse through file and search for string
    	**************************************************************/	
      i = 0; //prime the while "pump"
    
      while(1) {
        ch=fgetc(cfile);  
        if(ch == EOF)
          break;
        if(isalpha(ch)) {
          mystring[i] = ch;
          ++i;
        }
        else { //we should be at the end of the word
          //so set mystring's word, to all lowercase
          len = strlen(mystring);
          if(len == 0) 
            continue;
          for(j = 0; j <= len; j++) {
            if(mystring[j] >= 'A' && mystring[j] <= 'Z') 
              mystring[j] += ('a' - 'A');
          }
          //add the end of string char
          mystring[i+1] = '\0';
          //show nothing is going awry :)
          printf("\n mystring: %s", mystring);
          //see if the words are a match and count 'em up, if they are.
          if((strcmp(mystring, target)) == 0)
            count++;
          
          //remove the old word
          for(i = 0; i <= len; i++)
            mystring[i] = '\0';
          
          i = 0; //reset i for the next word
        }
      }
      //close file
      fclose(cfile);
    	
      //show user file has been written  
      //printf("\nSuccess. File has been written\n");
    	
      printf("\n\nYour search '%s' was found %d times \n", target, count);
    	
      printf("Press Enter to Continue...");
      j = getchar();
      return 0;
    }
    I used this phrase, in the file "blessing.txt":
    "For food, for raiment, for life and opportunity; for friendship and fellowship, we thank Thee, oh Lord."

    It works properly, in limited testing.
    Last edited by Adak; 12-01-2009 at 09:14 AM.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    26
    At the bottom of the text file, I need to print what they searched for and how many times it showed up. This is why I was using a+. Is this not correct?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    At the bottom of the text file, I need to print what they searched for and how many times it showed up. This is why I was using a+. Is this not correct?
    The file mode has nothing to do with printing out what the target was, or how many times it was found.

    You should use "r" for read mode, when you want to search through a file from front to back. Append mode is for writing onto the end of the file (appending the new data).

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a text file for double quotation marks "
    By willie in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 02:00 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 09:54 AM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 12:45 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21