Having some troubles. The idea is that if the file size is greater than 100 bytes, then the file is sought to the 100th to last byte. Then the file is printed to the screen. I'm getting a segfault at the fseek for >100 files, and a segfault at the getc. GDB isn't really helping explain why (for my knowledge base). If I call my list_file(Filename) function similar files (it cats them to stdout), the getc operates normally.
EDIT: Problem solved rookie mistake.
Here's my code:
Code:
void tail_file(char* filename)
{
//Check if size is smaller than 100 bytes
struct stat file_stats;
stat(filename, &file_stats);
int size = file_stats.st_size;
FILE *f;
if((fopen(filename, "r")) == NULL)
{
perror("The function 'fopen' failed");
return;
}
printf("opened the file!\n");
if(size > 100)
{
printf("size was greater than 100 \n");
if((fseek(f, -100, SEEK_END)) != 0) //ERROR
perror("The function 'fseek' failed");
}
char c;
while( (c = fgetc(f)) != EOF) //error in here
putchar(c);
fclose(f);
}
example of gdb output:
Program received signal SIGSEGV, Segmentation fault.
0xb7ea318d in getc () from /lib/tls/i686/cmov/libc.so.6
(gdb) where
#0 0xb7ea318d in getc () from /lib/tls/i686/cmov/libc.so.6
#1 0x0804898a in tail_file (filename=0xbfdbac1a "tee") at functions.c:132
#2 0x080489c1 in main (argc=Cannot access memory at address 0x11
) at functions.c:138