Thread: Help with searching text file

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    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.
    "r" will allow me to "read" and "write" to the file?

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    "r" will allow me to "read" and "write" to the file?
    No, only read.

    Do you want to write something to a file, as well?

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    No, only read.

    Do you want to write something to a file, as well?
    Yeah I need to print - at the bottom of the text file - the mystring and target strings. that is why i was leaning towards the a+ cuz I thought it appended text at the end of the file?

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    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.


    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.
    Hmm. Looks like a lot of stuff I haven't learned yet in C At this point I'm just trying to get my program to work as it is... I really appreciate your help.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK. You need to run the program I posted. The mystring words are printed up, one by one, as they are found, in the file.

    The target is also printed up.

    Neither action requires any data being written to a file.

    I *tried* to just get your program to work, but it has some major major problems. After about 1/2 hour of that, I just removed a bunch of it, and pulled some of your code, into a new loop, with some new code.

    Everything in there is as basic as I know how to make it. I could write up my own version of isalpha(), but why not use (and learn how to use), the functions in the standard C library?
    Last edited by Adak; 12-01-2009 at 09:26 AM.

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    OK. You need to run the program I posted. The mystring words are printed up, one by one, as they are found, in the file.

    The target is also printed up.

    Neither action requires any data being written to a file.
    The problem is you are using code that we haven't been introduced to in class. It would make my prof asking questions, and I wouldn't even understand what I'm learning ;(

    Is my code that crappy where it can't be fixed? I hope not

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you work with strings, you really have to do things step by step. You can't do things out of order, or overlook anything, because it's all sequentially handled. So if you make a mistake in step #3, everything past that will probably be garbage - not just the next step.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    The problem is you are using code that we haven't been introduced to in class. It would make my prof asking questions, and I wouldn't even understand what I'm learning ;(

    Is my code that crappy where it can't be fixed? I hope not
    It can be salvaged, but it will need some major re-arranging.

    Try and look at my program (well, our program since I did steal some of your code, too).

    Follow what it's doing:

    1) get everything declared
    2) open the file with the words
    3) get the target string
    4) set all letters in the target to lowercase
    5) decide on how you want to loop through the file, and on how you want to handle the words from the file.

    Everything has to go step by step - no jumping around!


  9. #24
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Hmm your program should work as is - after you have moved your "convert string to lower case" for your target. Also you were using the strlen(mystring) for this instead of strlen(target).

    Also using a+ argument for fopen, is not wrong, but is that your intent as you will keep appending your results to this file and your program will fscan that as well. The other issue was to move your "convert to lower" code for your target, and place that within your do-while loop. You were about 90% done here and mostly correct besides those two major issues. Please check this out:


    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
            **************************************************************/
    
      //compare our strings
      do
        {
          //scan through file
          fscanf (cfile, "%s", target);
    
      //convert string to lowercase
      for (i = 0; i < strlen(target); 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
        }
    
          //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
            }
          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;
    }

  10. #25
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by slingerland3g View Post
    Hmm your program should work as is - after you have moved your "convert string to lower case" for your target. Also you were using the strlen(mystring) for this instead of strlen(target).

    Also using a+ argument for fopen, is not wrong, but is that your intent as you will keep appending your results to this file and your program will fscan that as well. The other issue was to move your "convert to lower" code for your target, and place that within your do-while loop. You were about 90% done here and mostly correct besides those two major issues. Please check this out:


    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
            **************************************************************/
    
      //compare our strings
      do
        {
          //scan through file
          fscanf (cfile, "%s", target);
    
      //convert string to lowercase
      for (i = 0; i < strlen(target); 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
        }
    
          //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
            }
          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;
    }
    Thank you. I appreciate your help. However, I have used the search term 'the' and it does not display the number of times it was in there. The count looks like it should be working so I'm not sure what I'm doing wrong.

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    26
    Weird, but great news: I changed 'a+' to 'r+' and it now works perfect.

    Thank you so much for your kind help. Now I'm going to go through and see what I can fix to make it better. If you have thoughts on how to make this better, so I can learn for future programming, that would be great

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you use the append file mode to open a file, the file pointer is positioned at the very end of the file.

    Unless you're adding data to the end of a file, that's something you *don't* want, 99.9% of the time.

  13. #28
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    When you use the append file mode to open a file, the file pointer is positioned at the very end of the file.

    Unless you're adding data to the end of a file, that's something you *don't* want, 99.9% of the time.
    Ah! that makes perfect sense. Now it works perfectly with the 'r+'.

  14. #29
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by zacharyrs View Post
    Weird, but great news: I changed 'a+' to 'r+' and it now works perfect.

    Thank you so much for your kind help. Now I'm going to go through and see what I can fix to make it better. If you have thoughts on how to make this better, so I can learn for future programming, that would be great

    Yes, that is true if you have not closed the file handle. You will have to re-read the file in again to start a fresh search through. It worked in my case, using a+, since I went through only one iteration then closed the file handle.

  15. #30
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by slingerland3g View Post
    Yes, that is true if you have not closed the file handle. You will have to re-read the file in again to start a fresh search through. It worked in my case, using a+, since I went through only one iteration then closed the file handle.
    But multiple searches would require 'r+'?

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, 08: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, 11:45 AM
  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