asking a user to enter a filename(then reading from it)

so far it is not working

#include<stdio.h>
#include<stdlib.h>

int find_keyword(char[]);
int main()
{
char file_name[500];
char keyword[500];
int times_of_keyword;


printf("Please enter the filename\n");
scanf("%s",file_name);
printf("Please enter the keyword\n");
scanf("%s",keyword);

printf("%s\n",file_name);


FILE*f_name;
f_name=fopen(file_name,"r");//im not sure if this is correct

if(f_name!=NULL)//test to see if the file exist
{//begin if

while(!feof(f_name))

{//begin while
times_of_keyword=find_keyword(keyword);
printf("The number of times the keyword was found in the file are %d times",times_of_keyword);
fclose(f_name);

}//end while

}//end if

else
{//begin else
printf("The file could not be opened");
exit (0);
}//end else


getchar();
return 0;

}//end main
//program not completed
Last edited on
Couldn't you just compile it?

Anyway, it looks ok...but I'm not completely familiar with fopen().

Also, if you are using C++, I would suggest upgrading your entire program to iostream, fstream, and string.
You might run into some problems using a empty arrays for scanf() because the rest 400+ chars of the string still is being used by fopen. All c strings are ended by a \0 so try doing

for( int i = 0; i < 500; i++={
file_name[i] = '\0';
keyword[i] = '\0';
}

before scanf().
(This might be a bad solution, but its the only one i can think of)
Topic archived. No new replies allowed.