2

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

2 Answers2

1

The character reads the string of size BUFF_SIZE. So you need to iterate over each element in the string if BUFF_SIZE > 1 and must copy it into the s_line array to copy the complete string. Otherwise the characters will be skippped.

while ((ret = read(fd, character, BUFF_SIZE)) > 0)
{
  for(i=0;i<BUFF_SIZE;i++){ // for loop is added to visit every element of the character array
    if (character[i] == '\n'){
    s_line[len] = '\n';
    break;
    }
    s_line[len] = character[i];
    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;
    }
}}
j23
  • 3,139
  • 1
  • 6
  • 13
0

Try this one...

#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
// #include <stdio.h>

#ifndef BUFFER_SIZE
#define BUFFER_SIZE 42
#endif

char *get_next_line(int fd)
{
    char c;
    int i = 0;
    int rd_status = 0;
    char *buffer = malloc(10000);

    if (BUFFER_SIZE <= 0)
        return (free(buffer), NULL);
    while ((rd_status = read(fd, &c, 1)) > 0)
    {
        buffer[i++] = c;
        if (c == '\n')
            break;
    }
    if (rd_status == -1 || i == 0 || (!buffer[i - 1] && !rd_status))
    {
        return (free(buffer), NULL);
    }
    buffer[i] = '\0';
    return (buffer);

}

// int main (void)
// {
//  int fd = open("test", O_RDONLY);
//  char *line;
//  while ((line = get_next_line(fd)) != NULL)
//      printf("%s", line);
//  return (0);
// }

It doesn't use BUFFER_SIZE... But it looks like it does... I do not know everything about the read function, but I am pretty sure it will remember where it left off. Why do they want you to use a static buffer?

Disclaimer...(I have not tested this with multiple file descriptors), This project does pass (Edit: not) the norm, but it does pass the practice exam. ... ...

Edit: To Answer your question, you must read the file in question, and BUFFER_SIZE can only be defined once... Although i could interpret your question as asking if BUFFER_SIZE should be redefinable, in which case you should be using a code block like this, preferably near the top of your file or in a header file...

```
#ifndef BUFF_SIZE
#define BUFF_SIZE 515
#endif
```

Alternatively you may wish to use the macro BUFFER_SIZE which i believe is in the fcntl.h library, which can be redefined as you see fit, like this...

cc -Wall -Wextra -Werror -D BUFFER_SIZE=515 <files>.c
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34592325) – UpAndAdam Jun 30 '23 at 19:33