my function is reading character by character so that's why it is not working with multiple buffer size and l need help in fixing it so that it can work with any buffer size, not just 1.
so the main issue is it's supposed to work with buffer size not character by character
l have tried to read changing the buffer but it skips some characters
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#define BUFF_SIZE 32
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int get_next_line(int fd, char **line){
static char * s_line = NULL;
static int s_max = 0;
char character[BUFF_SIZE + 1];
if(s_line == NULL)
{
s_line = (char *)malloc((BUFF_SIZE + 1) * sizeof(char));
s_max = BUFF_SIZE;
}
int len = 0;
int ret;
while ((ret = read(fd, character, BUFF_SIZE)) > 0)
{
if (character[0] == '\n'){
break;
}
s_line[len] = character[0];
len = len + 1;
if (len >= s_max)
{
char *tmp;
s_max = s_max + BUFF_SIZE;
tmp = (char *) malloc((s_max + 1) * sizeof(char));
s_line[len] = '\0';
strcpy(tmp, s_line);
free(s_line);
s_line = tmp;
}
}
if (ret < 0) { //read error, free memory and return -1
free(s_line);
return -1;
}
if (len == 0){
free(s_line); //required to release the memory after reading the entire file
s_line = NULL;
return 0;
}
s_line[len] = '\0';
*line = s_line;
return 1;
}
int main(int argc, char **argv)
{
char *txt;
int fd;
fd = open(argv[1], O_RDONLY);
while (get_next_line(fd, &txt) > 0)
{
printf("%s\n", txt);
}
printf("Done\n");
if (txt != NULL)
{
free(txt);
}
return (0);
}
l expect the output of reading the whole file, not skipping characters like what it is doing