Originally Posted by
slingerland3g
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;
}