I fail to see the logic in that program, but apart from that, it has many issues:
- You are checking the variables before you have placed any valid data inside them.
- Don't use global variables or goto unless motivated. It isn't motived in your program, it just mess things up and makes the code unreadable.
- void main() isn't ANSI C. Use int main().
- Use variable names that make sense. Not s, t, x, y, z.
Here is the working code based on your program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STR_LEN 80
int main()
{
FILE* fp;
char key[MAX_STR_LEN];
char candidate[MAX_STR_LEN];
int count;
fp = fopen("data.txt","r");
if(fp == NULL)
{
printf("error message here");
exit(1);
}
printf("Enter string: ");
gets(key);
count=0;
while(!feof(fp)) /* while not end of file */
{
fscanf(fp, "%s", candidate);
if(strcmp(key, candidate) == 0)
count++;
}
fclose(fp);
printf("Found \"%s\" %d times.\n", key, count);
getchar();
return 0;
}