title: Read one line of a text file in C on Unix — my read_line is broken?
body: <p>I want to make a function that reads a line of your choice, from a given text file. Moving on to the function as parameters (<code>int fd</code> of the open, and <code>int line_number</code>)
  It must do so using the language C and Unix system calls (<code>read</code> and / or <code>open</code>).
  It should also read any spaces, and it must not have real limits (ie the line must be able to have a length of your choice).
  The function I did is this:</p>
  
  <pre><code>char* read_line(int file, int numero_riga){
      char myb[1];
      if (numero_riga &lt; 1) {
          return NULL;
      }
      char* myb2 = malloc(sizeof(char)*100);
      memset(myb2, 0, sizeof(char));
      ssize_t n;
      int i = 1;
      while (i &lt; numero_riga) {
          if((n = read(file, myb, 1)) == -1){
              perror("read fail");
              exit(EXIT_FAILURE);
          }
          if (strncmp(myb, "\n", 1) == 0) {
              i++;
          }else if (n == 0){
              return NULL;
          }
      }
      numero_riga++;
      int j = 0;
      while (i &lt; numero_riga) {
          ssize_t n = read(file, myb, 1);
          if (strncmp(myb, "\n", 1) == 0) {
              i++;
          }else if (n == 0){
              return myb2;
          }else{
              myb2[j] = myb[0];
              j++;
          }
      }
  
      return myb2;
  }
  </code></pre>
  
  <p>Until recently, I thought that this would work but it really has some problems.
  Using message queues, the string read by the <code>read_line</code> is received as a void string ( "\0" ). I know the message queues are not the problem because trying to pass a normal string did not create the problem.
  If possible I would like a fix with explanation of why I should correct it in a certain way. This is because if I do not understand my mistakes I risk repeating them in the future.</p>
  
  <p>EDIT 1. Based upon the answers I decided to add some questions.
  How do I end myb2? Can someone give me an example based on my code?
  How do I know in advance the amount of characters that make up a line of txt to read?</p>
  
  <p>EDIT 2. I don't know the number of char the line have so I don't know how many char to allocate; that's why I use <code>*100</code>.</p>