diff options
-rw-r--r-- | 7ed.h | 2 | ||||
-rw-r--r-- | functions.c | 36 | ||||
-rw-r--r-- | startmode.c | 2 |
3 files changed, 36 insertions, 4 deletions
@@ -52,6 +52,6 @@ int startmode(char filename[]); #ifndef PRINT_7ED_H #define PRINT_7ED_H -int print_7ed(); +int print_7ed(char filename[], long focus); #endif /* PRINT_7ED_H */ diff --git a/functions.c b/functions.c index f381dc8..15773bf 100644 --- a/functions.c +++ b/functions.c @@ -120,10 +120,42 @@ void shuffle(char arr[], int n) { } } -int print_7ed() { +int print_7ed(char filename[],long focus) { + + size_t line_count = 0; // Counter starting at 0 + FILE *file; + file = fopen(filename,"rb"); // Open file + + if (file == NULL) { // Check if you can open file + fprintf(stderr, "Cannot open file.\n"); + return 1; + } - printf("print_7ed()\n"); + char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4 + int breakflag = 1; + int chbuf_i; + while (1) { + size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes. + for (size_t i = 0 ; i < bytes_read; i++) { + if (buffer[i] == '\n') { // Searches through the buffer for newlines. + line_count++; + if (line_count == (size_t)focus) { //When the line count is the same as the focus + //printf("%ld\n", line_count); // The program will save the position. + breakflag = 0; + chbuf_i = i; + + break; + } + } + } + if (feof(file) || breakflag == 0) { // If end of file is encountered then break + break; + } + } + + fclose(file); + return 0; }
\ No newline at end of file diff --git a/startmode.c b/startmode.c index 7c1e5fd..dc71ffa 100644 --- a/startmode.c +++ b/startmode.c @@ -71,7 +71,7 @@ int startmode(char filename[]) { break; case 'P': case 'p': - print_7ed(); + print_7ed(filename, focus); break; case 'E': |