0

Hello I have to code a function that needs to write the line of our file until it meets '\n'.

But I am stuck , i dont understand how to use read function even after reading man/ looking other read examples.

My main function goes like that:

char *get_next_line(int fd);

All I have is fd.

I don't want to write the function above right now , I just want to understand how I can write a read function that will read from fd so I can make some tests to understand more.

I have only access to Read/Malloc/Free function.

ssize_t bytes_read;
char    *buffer;

buffer = (char *)malloc(6 + sizeof(char));
if (!buffer)
    return (0);
bytes_read = read(fd, buffer, 6);`

I've seen examples like that but how to access the information , what is the use of the bytes_read variable and can I access it later ???

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Pri
  • 1
  • Did you read the documentation of the `read` function? – Konrad Rudolph May 25 '23 at 10:20
  • The `read` function is not well suited to this problem. It's a rather artificial, impractical exercise. By far the easiest thing to do will be to use `read` to emulate an inefficient `getchar`, by calling `read(fd, &c, 1)`. – Steve Summit May 25 '23 at 10:20
  • @SteveSummit Why not? Or rather, what would you use instead to read from a file descriptor? `fdopen` + `FILE*` functions? – Konrad Rudolph May 25 '23 at 10:20
  • 1
    https://stackoverflow.com/q/3501338 – Robert Harvey May 25 '23 at 10:20
  • You are asking to be taught how to write C code. That's not what this site is for - Stackoverflow is meant to provide answers to specific questions, not fundamental concepts like what a return value is (the `bytes_read` variable) or how to read it. – Andrew Henle May 25 '23 at 10:21
  • 2
    Side note: `malloc(6 + sizeof(char));` is probably not what you want. – Steve Summit May 25 '23 at 10:22
  • @KonradRudolph There's only one function that reads from a file descriptor, of course. But it's not practical to use it to read one line, as requested here. If I need to read lines, I'll either use ``, or a buffering mechanism of my own. – Steve Summit May 25 '23 at 10:23
  • @Steve Right, the way I understand OP their assignment is to write that buffering mechanism themselves (I'm assuming this is a homework/… task). – Konrad Rudolph May 25 '23 at 10:24

1 Answers1

0

There's a fundamental problem with read function: It just reads any arbitrary bytes, doesn't differentiate on their values, thus doesn't recognise a newline either.

So on implementing the function you have handle the newlines being somewhere right in the middle of the data you just read from file. Additionally read doesn't necessarily read all the bytes that might fit into the buffer, so you might need to read once more to get an entire line.

Finally if no newline is encountered after filling the entire buffer you might need to re-allocate a larger buffer (you might want to ask if realloc is allowed, too. In a first step you might just ignore this case, though, and provide a statically allocated buffer with some reasonable size, hoping you won't exceed that one...

This might look somewhat like:

static char buffer[1024];
static size_t length = 0;
for(;;)
{
    // handle input

    ssize_t n = read(fd, buffer + length, sizeof(buffer) - length);
    if(n <= 0)
    {
        // something went wrong, handle appropriately;
        // this includes the case of reaching the end of the file,
        // in that specific case you'd return all the remaining buffer
        // and on that begin empty a null pointer.
    }
}

You might notice that this code handles the input before reading a new line – background is that you might have read more than one complete line at once, and you might no be looking for the next line still in the buffer!

So once you found a newline you'll allocate a new buffer for a string and copy the bytes from the beginning of the buffer up to the newline character found. Then you copy all the remaining bytes of the buffer towards front and read into the buffer starting right behind the bytes yet remaining.

Note that - as you rely on static (or alternatively global) variables – the function as is is not thread-safe. If that gets an issue you'll need to introduce some appropriate protection.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59