Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions get_next_lineFI/get_next_line.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "get_next_line.h"

char *get_next_line(const int fd)
Expand Down Expand Up @@ -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);
Expand All @@ -43,30 +39,30 @@ char *get_next_line(const int fd)
// #include <fcntl.h> // For open()
// #include <unistd.h> // 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;
}
5 changes: 4 additions & 1 deletion get_next_lineFI/get_next_line.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "libft/libft.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#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);
Expand Down
24 changes: 8 additions & 16 deletions get_next_lineFI/get_next_line_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down