If everything went alright the program will return 0. The results are captured to do some basic error-handling in the if statement that follows. Iterate through the array and find the frequency of each word and compare the. The actual work is done in the next statement where we call a second function called Search_in_File that uses our command-line input. Split a line at a time and store in an array. When you open those files, you'll see all the contents within the file as plain text.
You can easily create text files using any simple text editors such as Notepad. The system() function can execute a system specific command. When dealing with files, there are two types of files you should know about: Text files Binary files 1.
With the system(“cls”) function on windows and system(“clear”) function on Unix/Linux we can clear our screen, so we can display the results on a fresh screen. However, I am running the script over multiple files using a loop. To read more on command-line parsing read the following tutorials: C Tutorial – More on Functions and C Tutorial – Command Line Parameter Parsing. Learn more about read text, specific words and values. (Note: that the first command-line parameter is the program itself.) We just say if the command-line parameters are smaller or larger than three, display our usage function and exit the program. We also want to make sure that we get a proper usage message, if the command-line options are not inputted correctly. This why we use int main(int argc, char *argv). Result = Search_in_File(argv, argv) īecause we want to write a program that can that can be used over and over, we need to make sure that all the variables we need, such as filename and the search-string, can be inputted at the command-line.