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