line 43, the scanf() is wrong. Use "%s" (for string), not "%c" (for a single character). scanf() will not work at all if there are spaces in the filename. In that case call fgets() instead of scanf(), e.g. fgets(filename, sizeof(filename), stdin); Then you will have to remove the '\n' that fgets() puts at the end of the string.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I need to basically rewrite grep command:
method is suppose to take an exact string and an exact filename as the 2 arguments
grep
then it has to search through the file for the string and print out the "line" and the "line number" the string is in.
example #grep matt students
12 the student matt was late for class today
35 when class started matt was just walking in the door
Im using some code from a website.
There's your first problem. You don't know C well enough to modify existing code to try using someone else's code as a starting point. Therefore, start over and take it a step at a time.
1) Input, and display the 2 values. When this runs without error...
2) Open the file name specified in the FILENAME parameter. When you get this correct (complete with error processing)...
3) Read the file line by line in a loop and display each line.
Continue until the program is running as you wish.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944