-1

I'm trying to use the functions read() and write() from unistd.h, but whenever I try input anything, it does not work. And I am only alowed to use functions from fcntl.h and unistd.h, not those from stdio.h.

Here is my code:

#include <fcntl.h> 
#include <unistd.h> 

int main() {
    int fd_in = open("/dev/pts/5", O_RDONLY);
    int fd_write = open("/dev/pts/log.txt", O_RDWR);
   
    char buf[20];
    ssize_t bytes_read;

    if (fd_in == -1){
      char out[] = "Error in opening file";
      write(fd_write, out, sizeof(out));
    }
    
    //using a while loop to read from input
    while ((bytes_read = read(fd_in, buf, sizeof(buf))) > 0) {
         char msg[] = "Block read: \n<%s>\n";
         read(fd_write, msg, sizeof(msg));
    
    //continue with other parts 
    }
}

The problem is that I don't get the desired output for the inputs I provide. For example:

//input 
Hello 

//output
Block read: 
<Hello> 
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Gabi
  • 9
  • 4
  • 1
    Do you know what the directory `/dev/pts/` means? Do you think it makes sense for it to contain a file called `log.txt`? What happens if `open`ing that fails? Do you know why it's important to save the return values of `read` and `write`? What function do you think handles sequences like `%s`? – Joseph Sible-Reinstate Monica Oct 25 '22 at 02:07
  • 1
    Try to convince your rubber duck that `read(fd_write, msg, sizeof(msg));` does what you want it to do. – n. m. could be an AI Oct 25 '22 at 02:52
  • @Gabi, given your requirement not to use `stdio.h`, these may be interesting thoughts to you: https://stackoverflow.com/questions/11213031/write-or-printf-which-is-faster – user20276305 Oct 28 '22 at 22:59

1 Answers1

0

I wrote example code how to use read(2) and write(2). I don't know whether you need to use /dev/pts/ or not. I never used it, so also now I don't use it. Maybe my example will be helpful anyway.

The header string.h is included only for strlen(3).

#include <unistd.h>
#include <string.h>

int main (void) {
    size_t input_size = 50;
    // "+ 1" is for storing '\0'
    char buffer[input_size + 1];
    // We don't use the return value of
    //  memset(3), but it's good to know
    //  anyway that there is one. See also
    //  https://stackoverflow.com/q/13720428/20276305
    memset(buffer, '\0', input_size + 1);
    ssize_t bytes_read_count = -1;
    ssize_t bytes_written_count = -1;

    // Reading
    bytes_read_count = read(STDIN_FILENO,
                            buffer,
                            input_size);
    if (bytes_read_count == -1) {
        // No return value checking (and also below). It
        //  would make little sense here since we exit the
        //  function directly after write(2), no matter if
        //  write(2) succeeded or not
        write(STDERR_FILENO, "Error1\n", strlen("Error1\n"));
        return 1;
    }
    // We want to be sure to have a proper string, just in
    //  case we would like to perform more operations on it
    //  one day. So, we need to explicitly end the array
    //  with '\0'. We need to do it regardless of the earlier
    //  memset(3) call because the user might input more
    //  than input_size, so all the '\0' would be
    //  overwritten
    buffer[input_size] = '\0';

    // Writing
    bytes_written_count = write(STDOUT_FILENO,
                                buffer,
                                bytes_read_count);
    if (bytes_written_count == -1) {
        write(STDERR_FILENO, "Error2\n", strlen("Error2\n"));
        return 1;
    }

    return 0;
}

Edit: I add a comment about memset(3) return value, and also remove checking it since it seemed unnecessary.

user20276305
  • 95
  • 1
  • 7