i was given the assignment in college to write a program in c that will run from the command prompt and will search a desired text file for a desired word heres the code that i have and i think it should work but then im not sure so can any one help me with this
co the idea is that if my text file says "This is a text file and it can be searched"Code:#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int ch; char string[150]; char *tokenptr; FILE *in_stream; if(argc==3) { if((in_stream=fopen(argv[1],"r"))==NULL) { fprintf(stderr,"Could Not Open File!"); perror("Because"); return 1; fclose(in_stream); } else { while(!feof(in_stream)) { fgets(string,150,in_stream); printf("%s\n\n",string); tokenptr=strtok(string," "); while(tokenptr!=NULL) { ch=strcmp(tokenptr,argv[2]); if(ch==0) { printf("%s was Found In File",argv[2]); } else { printf("%s was not found in the file",argv[2]); } fclose(in_stream); return 0; } } } if(argc!=3) return 1; } }
and at the dos window i typewhy does the reply always come back saying it cant find the wordCode:C:\prog\debug\c_find mytextfile.txt can
ps its called c_find as not to confuse my self with the dos command and im using vc6 if that helps



LinkBack URL
About LinkBacks


