Question
Task 3 (25 points) Modify head.c so it will display the last 10 lines of a file along with the line numbers . Make sure
Task 3 (25 points)
Modify head.c so it will display the last 10 lines of a file along with the line numbers. Make sure your program can handle file that has less than 10 lines gracefully. Name your source file task3.c. You program should work like this. hb117@uxb4:~$ gcc -Wall task3.c -o task3 hb117@uxbt:~$ ./task3 TheStarSpangledBanner.txt 26 Oer the land of the free and the home of the brave. 27 28 O thus be it ever when freemen shall stand 29 Between their lovd home and the wars desolation! 30 Blest with victry and peace may the heavn rescued land 31 Praise the power that hath made and preservd us a nation! 32 Then conquer we must, when our cause it is just, 33 And this be our motto - In God is our trust, 34 And the star-spangled banner in triumph shall wave 35 Oer the land of the free and the home of the brave.
5 additional points will be added if your solution only reads the file once.
Here is head.c:
#include#include #include int main(int argc, char *argv[]) { if(argc!=2){ return 1; } char *filename = argv[1]; FILE *input = fopen(filename,"r"); if(input==NULL){ printf("Cannot open %s: %s ", filename, strerror(errno)); return 1; } char line[BUFSIZ]; int count=0; while(1){ fgets(line, BUFSIZ, input); printf("%s", line); count++; if(count==10) break; } return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started