I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this?
I actually wanted to do it the C way. Yeah, it would be a lot easier using shell My attempt is to keep only the final 100 lines in a text file, and throw the rest of the content, not meant for display only. I've searched through some books and website and found nothing so far.
Err well, typically you would identify a line by the presence of the '\n' character.
So, open the file. Go the end of the file. Work backwards towards the start of the file, counting the number of '\n'. When you have reached the required number of '\n' characters, store the position in the file.
Read from the stored position of the file to the end of file into memory buffer. Dump buffer contents into file.
Or something like that ...
Some useful functions are fopen, fclose, fread and fwrite.
I've tried out the code but it seems the libraries are not compatible to my unix system. It kept errored out with the "Function prototypes are an ANSI feature" and "Incompatible types in cast: Must cast from scalar to scalar or to void type". I am just at the beginner level, perhaps, you can give me a guide of what is it all about?
Oops... I played around with the code to make it work but failed to do so. I changed the type defined. Now, I am not able to duplicate the error now cos it does not look like the original file at all i just wonder, izzit a library compatibality problem that makes me unable to compile the code?