From dfad11647c8ba58ec6912a3baf6c08e1326a28af Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 26 Oct 2023 21:28:37 +0200 Subject: Completely broken. Do not touch --- functions.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'functions.c') 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 -- cgit v1.2.3