need help with c

hello guys

i am trying to code a program that will read the last line from a file using c language. i got no experience with c, so my code is giving me plenty of errors. could you help me debug ??

code:

 
# include <stdio.h>
 
typedef int function;

void main(void){
int count = 0;
char *numbers;
function x;
 
  while ( count < 200){
    count++;
    x = readlastline(numbers);
  }
}
 
function readlastline(char *f_name){
 
FILE *f_ptr = fopen(f_name, "r");
long first_l = fseek(f_ptr, 0);
int p = -1;
char t = " ";
 
 while (t != "\n"){
  fseek(f_ptr, p, SEEK_END);
  if(ftell(f_ptr) == first_l){
   break;
  }
  t = fgetc(f_ptr);
  p = p - 1;
 }
 t = fgets(f_ptr);
 fclose(f_ptr);
 return t;
}

Much love

#include<stdio.h>

FILE *file_pointer();

main()
{

        char line[80];
        FILE *last=file_pointer();
        fgets (line,80,last) ; // Reading the last line
        printf ( "%s",line ) ; // displaying the last line
}
FILE *file_pointer()
{
        FILE *fp ;
        fp = fopen ("text","r") ;  // Open the file
        fseek(fp,0L,SEEK_END);    // Seek the file pointer to end
        int last = ftell ( fp ) ;
        fseek ( fp,0L,SEEK_SET ) ;
        int first =ftell (fp ) ;
        int i = 0;
        int c ;
        int len = last - first ;
        while ( i++ < len  )        // Doing the operation till file start
        {
                fseek(fp,-i,SEEK_END); // Seeking the file pointer to first from end
                c =fgetc (fp);         // Getting the character for checking new line
                if ( c=='\n')          // If it is new line , Getting the character
                                       // Till next new line character
                {
                        while ( i++ < len )
                        {
                        fseek ( fp , -i , SEEK_END ) ;
                        if ( (c=fgetc( fp)) == '\n' )
                        {
                                return fp ; // Returning the file pointer

                        }

                        }
                }
        }
}



 

thanks pavun

but there is still a problem, your code is giving me error for the following line " int last = ftell(fp)", it says " int not expected ". how could it be when ftell returns a long ?

Actually It is working fine in my environment .
I think it could be , because of different type of data types.
ftell return long , But I stored in int . You change the data type of
first , last variable into long date type , It will work.

Thanks

minix can be very annoying sometimes ! i have changed both last and first into long. but, it's still giving me the same error. now it says " long not expected "

your code looks right to me

thankx bro

Might be a strange idea, but try changing the comments started by // into /* */ comments, eg

// Seek the file pointer to end

to

/* Seek the file pointer to end */

The reason is this: correct ANSI C doesn't support the double-slash comments, since those were originally introduced by C++. So if your compiler is very strict about this (and you being on Minix this might well be) it will fail on those.

fseek is used for binary files to change the file position indicator in the stream, to read the last line you can simply read the file untill EOF with fgets.

i have changed the comment style , but i am still having the same problem

minix = assassin

please help me guys

 

#include<stdio.h>
#include<string.h>

main()
{
        FILE *fp ;
        char line[80];
        char temp[80];
        fp = fopen ("text","r") ;  // Open the file
        while (fgets (line,80,fp)!= NULL );
        printf ( "%s",line ) ; // displaying the last line
}

That's not true? ANSI as adopted ISO/IEC 9899:1999, and // comment style is supported by ISO C99.

I know, people use to think of "ANSI C" as ISO/IEC 9899:1990, but this is strictly speaking wrong since many years :wink:

Cheers,
Lo�c