I am trying to print last n bytes from a file. This is my code. I got segmentation fault. Please someone tell what is the error? Following is the code-
#include <stdio.h>
#include <ctype.h>
void fileprint(int line,char s[]); //to print last n lines
int atof(char s[]);
int main(int argc, char * argv[]) // tail filename 10
{
if(argc == 1)
printf("usage: tail");
if(argc == 2)
fileprint(10, argv[1]);
if(argc == 3)
fileprint(atof(argv[2]), argv[1]);
if(argc > 3)
printf("usage: tail ");
}
void fileprint(int line, char s[])
{
int c;
FILE * p = fopen(s, "r"); // pointer to file
fseek(p, -line, SEEK_END);
while((c = fgetc(p)) != EOF) {
putchar(c);
}
}
int atof(char s[]) //convert string int
{
int i,n;
for(n = 0;isdigit(s[i]); i++)
n = 10 * n + (s[i] - '0');
return n;
}