From 07e70a5136d57ad27b9fb7f0b0c52e00827ac160 Mon Sep 17 00:00:00 2001 From: Atilla Idris <136346643+Idriz4work@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:19:22 +0100 Subject: [PATCH] Add files via upload --- get_next_lineFI/get_next_line.c | 60 +++++++++++++-------------- get_next_lineFI/get_next_line.h | 5 ++- get_next_lineFI/get_next_line_utils.c | 24 ++++------- 3 files changed, 40 insertions(+), 49 deletions(-) diff --git a/get_next_lineFI/get_next_line.c b/get_next_lineFI/get_next_line.c index d9df6e5..5a37a82 100644 --- a/get_next_lineFI/get_next_line.c +++ b/get_next_lineFI/get_next_line.c @@ -1,7 +1,3 @@ -#include -#include -#include -#include #include "get_next_line.h" char *get_next_line(const int fd) @@ -31,7 +27,7 @@ char *get_next_line(const int fd) if (result <= 0 && (!valueholder || !*valueholder)) { freeler(&valueholder, NULL, NULL); - return NULL; // Indicate EOF or error + return NULL; } line = insert_line(&valueholder); @@ -43,30 +39,30 @@ char *get_next_line(const int fd) // #include // For open() // #include // For close() -// int main(void) -// { -// int fd; -// char *line; -// int lines_read = 0; -// // Open a file to test with -// fd = open("gnlTester-master/files/42_with_nl", O_RDONLY); -// if (fd == -1) -// { -// perror("Error opening file"); -// return 1; -// } -// // Keep reading lines until EOF or an error occurs -// while ((line = get_next_line(fd)) != NULL) -// { -// printf("Line %d: %s", lines_read, line); -// // printf("\n"); -// lines_read++; -// free(line); // Free the line returned by get_next_line -// } -// // Check if the last call to get_next_line failed -// if (line == NULL && lines_read == 0) -// printf("No lines read or error occurred.\n"); -// // Close the file -// close(fd); -// return 0; -// } +int main(void) +{ + int fd; + char *line; + int lines_read = 0; + // Open a file to test with + fd = open("gnlTester-master/files/42_with_nl", O_RDONLY); + if (fd == -1) + { + perror("Error opening file"); + return 1; + } + // Keep reading lines until EOF or an error occurs + while ((line = get_next_line(fd)) != NULL) + { + printf("Line %d: %s", lines_read, line); + // printf("\n"); + lines_read++; + free(line); // Free the line returned by get_next_line + } + // Check if the last call to get_next_line failed + if (line == NULL && lines_read == 0) + printf("No lines read or error occurred.\n"); + // Close the file + close(fd); + return 0; +} diff --git a/get_next_lineFI/get_next_line.h b/get_next_lineFI/get_next_line.h index 1031450..347870d 100644 --- a/get_next_lineFI/get_next_line.h +++ b/get_next_lineFI/get_next_line.h @@ -1,8 +1,11 @@ #include "libft/libft.h" +#include +#include +#include +#include #define BUFF_SIZE 42 -char *get_next_line(const int fd); char *insert_line(char **valueholder); void update_holder(char **valueholder); int freeler(char **s1, char **s2, char **s3); diff --git a/get_next_lineFI/get_next_line_utils.c b/get_next_lineFI/get_next_line_utils.c index 943b706..fc722d3 100644 --- a/get_next_lineFI/get_next_line_utils.c +++ b/get_next_lineFI/get_next_line_utils.c @@ -90,13 +90,10 @@ void update_holder(char **valueholder) // Inserts a line into the line buffer char *insert_line(char **valueholder) { - int i; - int j; - char *line; - char *indicator; + int i, j; + char *line,*indicator; - i = 0; - j = 0; + j = i = 0; line = (char *)malloc(ft_strlen(*valueholder) + 1); if (line == NULL) return NULL; @@ -107,16 +104,13 @@ char *insert_line(char **valueholder) line[j++] = (*valueholder)[i++]; line[j++] = (*valueholder)[i++]; line[j] = '\0'; - } - else - { - while ((*valueholder)[i] != '\0') - line[j++] = (*valueholder)[i++]; - line[j] = '\0'; - freeler(valueholder, NULL, NULL); + update_holder(valueholder); return line; } - update_holder(valueholder); + while ((*valueholder)[i] != '\0') + line[j++] = (*valueholder)[i++]; + line[j] = '\0'; + freeler(valueholder, NULL, NULL); return line; } @@ -137,8 +131,6 @@ int read_file(char **valueholder, int bytes, int fd) free(temp); free(*valueholder); *valueholder = newline; - if (*valueholder == NULL) - return -1; if (strchr(*valueholder, '\n')) return 1; }