C PROGRAMMING: Write some code that prints out the last N lines of a given file
ID: 642841 • Letter: C
Question
C PROGRAMMING:
Write some code that prints out the last N lines of a given file
Arguments to your program will be passed on the command line in the following order
1. Name of the file
2. N
N will always be at least 1
If N is greater than the number of lines in the file, all of the lines in the file should be displayed
You may assume that no line is longer than 100 characters
For an additional challenge try to solve the problem where there is no limit on the length of a line
solution to dealing with any length lines can involve using the functions ftell and fseek.
There is no limit to the number of lines in a file
Some lines may only contain the newline character, these still count as a line
My editor automatically added 2 newline characters to the ends of most of the test files but does not display them. Your editor may or may not display them but do be aware that they are there.
Example. Assume the file meme.txt contains the following
It
is
over 9000!!!
1../tail.out 1 over 9000!!!
2../tail.out 2 is
over 9000!!! 3../tail.out 10
It
is
over 9000!!!
Explanation / Answer
#include #include #include #include #include #include #include #include "fileOperating.h" #include void createFile(char * fileName) { FILE * myfile=fopen(fileName, "w+"); fclose(myfile); } void emptyFile(char * fileName) { FILE * myfile=fopen(fileName, "w+"); fclose(myfile); } int searchStrInFile(char * FileName,char * searchstr) { int len1=0; int *loc= findAllLine(FileName,&len1); int count=0; char * str; int start; int len; int lineLen=0; int findLine=-1; while(countRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.