summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-12-29 10:36:12 -0700
committermo khan <mo@mokhan.ca>2015-12-29 10:36:12 -0700
commit61d51ca13eea2b16fc4fde2fb1926e953c350168 (patch)
tree89dcacf2b425cb5311b7feb867f3a1cc02c0e685
parent228c8513cf9830746e87b280f6f7890a351be4a3 (diff)
tail the last 10 lines of a file.
-rw-r--r--tail.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/tail.c b/tail.c
index e92992d..abd57c8 100644
--- a/tail.c
+++ b/tail.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
int main(int argc, const char *argv[])
{
@@ -6,15 +7,20 @@ int main(int argc, const char *argv[])
if (file) {
int current_position = ftell(file);
int character;
+ int lines_found = 0;
+ int lines_to_print = 10;
fseek(file, 0L, SEEK_END);
current_position = ftell(file);
- printf("file size: %d\n", current_position);
+ while(--current_position >= 0) {
+ fseek(file, current_position, SEEK_SET);
+ if (getc(file) == '\n' && ++lines_found == lines_to_print) {
+ break;
+ }
+ }
- while(current_position >= 0) {
- fseek(file, --current_position, SEEK_SET);
- character = getc(file);
+ while( (character = getc(file)) != EOF) {
putchar(character);
}