This is a discussion on Help with searching text file within the C Programming forums, part of the General Programming Boards category; Originally Posted by Adak The file mode has nothing to do with printing out what the target was, or how ...
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 08:26 AM.
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.
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!
![]()
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; }
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![]()
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.