need help with c


 
Thread Tools Search this Thread
Top Forums Programming need help with c
# 1  
Old 02-25-2010
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:

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
# 2  
Old 02-26-2010
Getting the last line of the file

Quote:

Use this code to get the last line of the file .
I have written the function for finding the last line file pointer
Code:
#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

                        }

                        }
                }
        }
}


Last edited by pavun_cool; 02-26-2010 at 01:19 AM.. Reason: Adding some statements
# 3  
Old 02-26-2010
getting the last line

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 ?
# 4  
Old 02-26-2010
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
# 5  
Old 02-26-2010
getting the last line

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
# 6  
Old 02-26-2010
Might be a strange idea, but try changing the comments started by // into /* */ comments, eg
Code:
// Seek the file pointer to end

to
Code:
/* 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.
# 7  
Old 02-26-2010
Quote:
Originally Posted by surubi_abada
hello guys

i am trying to code a program that will read the last line from a file using c language.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question