diff options
author | Oskar <[email protected]> | 2023-10-26 21:28:37 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2023-10-26 21:28:37 +0200 |
commit | dfad11647c8ba58ec6912a3baf6c08e1326a28af (patch) | |
tree | 24ff8e5e5764bfee8de60f08ae36f94de27b8a58 /functions.c | |
parent | b192136b7672886b03f509993825915132c16fc6 (diff) |
Completely broken. Do not touch
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 36 |
1 files changed, 34 insertions, 2 deletions
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 |