how to find tail of a file using c code -


i wrote code find tail, but, has 1 exception, when trying print whole lines tail, not print starting character. e.g. if file having 14 lines, , value of n entered 14, not print starting character.

please modify code:

#include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) {     file *in, *out;     int count = 0,lines;     long int pos;     char s[100];     char ch,c;     if(argc<2)     {         printf("arguments passed should 2");     }     else if(argc>2)     {         printf("too many argumnets passed");     }     in = fopen("anj.txt", "r");     out = fopen("output.txt", "w");     if(in==null || out==null)     {         printf("unable open");         exit(0);     }     else     {         int n=atoi(argv[1]);         if(n>=1)         {             fseek(in, 0, seek_end);             pos = ftell(in);             while (pos)             {                 fseek(in, --pos, seek_set);                 if (fgetc(in) == '\n')                 {                     //count=count+1;                     if (count++==n)                     break;                 }             }             if(count<n)             {                 printf("no. of lines in file %d less enterd",count);             }             c = fgetc(in);             while (c != eof)             {                 fputc(c, out);                 c = fgetc(in);             }         }         else         printf("renter value of n");     }     fclose(in);     fclose(out);     return 0; } 

if number of lines same entered value, code read first char in file, consume fgetc, , end loop due pos == 0!

add check after while loop corner case:

if (pos == 0) {     fseek(in, 0, seek_set); } 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -