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



LinkBack URL
About LinkBacks




