Well im trying to find a string in a textfile but i have some problems :
When im trying to create a string called temp in the Compare function using the length of the string that was typed in to main to compiler gives me an error , and even if ill type in the code the size of temp the program will still not work . any1 have any ideas ?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void Compare(FILE *filein,char *str,int length,char *source)
 char temp[length];
 fgets (temp,length,filein);
 if (strcmp (temp,str) == 0)
  printf ("The string : %s was found in the source file %s.\n",str,source);
  printf ("Press any key to terminate the program...\n");
int main()
 FILE *fin;
 char str[40];
 char source[40];
 char ch;
 int length;
 printf ("Enter the string the you are looking for: ");
 printf ("Enter the source file that will be searched: ");
 fin = fopen(source,"rt");
 length = strlen(str);
 if (fin == NULL)
  printf ("Failed to open source file..\n");
 while (ch!=EOF)
  ch = fgetc(fin);
  if (ch == str[0])
 printf ("The string %s was not found under the file %s\n",str,source);
 printf ("Press any key to terminate the program...\n");
 return 0;

char temp[length];
the error is that length is not a
constant known at compile-time.

(this is not an error in C99, but
is an error in standard C.)

A couple other things.
See this about gets().
See this about formatting. You need indentation and whitespace.

After you read the first character, then read the rest of the line, the rest of the line does not contain the first character. so the compare never works. Why not just use fgets() to read a full line then use strstr() to search that line?

And getch() is not standard, therefore not portable. You should use getchar() instead.

This article has been dead for over six months. Start a new discussion instead.