informatica:sol:laboratorio20:esercitazionib:readnwriten

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisioneRevisione precedente
Prossima revisione
Revisione precedente
informatica:sol:laboratorio20:esercitazionib:readnwriten [03/05/2020 alle 07:45 (5 anni fa)] Massimo Torquatiinformatica:sol:laboratorio20:esercitazionib:readnwriten [03/05/2020 alle 10:44 (5 anni fa)] (versione attuale) Massimo Torquati
Linea 1: Linea 1:
 ===== Implementazione delle funzioni "readn" e "writen" ==== ===== Implementazione delle funzioni "readn" e "writen" ====
-(tratto da "Advanced Programming In the UNIX Environment" 3rd Edition by W. Richard Stevens and Stephen A. Rago, 2013, Addison-Wesley)+(tratto da "Advanced Programming In the UNIX Environment" by W. Richard Stevens and Stephen A. Rago, 2013, 3rd Edition, Addison-Wesley)
  
  
Linea 24: Linea 24:
 handle a return value that’s possibly less than requested. These two functions simply handle a return value that’s possibly less than requested. These two functions simply
 call read or write as many times as required to read or write the entire N bytes of call read or write as many times as required to read or write the entire N bytes of
-data.+data.\\ 
 +We call writen whenever we’re writing to one of the file types that we mentioned, 
 +but we call readn only when we know ahead of time that we will be receiving a certain 
 +number of bytes. Figure 14.24 shows implementations of readn and writen that we 
 +will use in later examples. 
 +Note that if we encounter an error and have previously read or written any data, we 
 +return the amount of data transferred instead of the error. Similarly, if we reach the end 
 +of file while reading, we return the number of bytes copied to the caller’s buffer if we 
 +already read some data successfully and have not yet satisfied the amount requested.
  
 <code c> <code c>
 ssize_t  /* Read "n" bytes from a descriptor */ ssize_t  /* Read "n" bytes from a descriptor */
-writen(int fd, void *ptr, size_t n) +readn(int fd, void *ptr, size_t n) {  
-{  +
    size_t   nleft;    size_t   nleft;
    ssize_t  nread;    ssize_t  nread;
Linea 35: Linea 42:
    nleft = n;    nleft = n;
    while (nleft > 0) {    while (nleft > 0) {
-    +     if((nread = read(fd, ptr, nleft)) < 0) { 
-   +        if (nleft == n) return -1; /* error, return -1 */ 
 +        else break; /* error, return amount read so far */ 
 +     } else if (nread == 0) break; /* EOF */ 
 +     nleft -= nread; 
 +     ptr   += nread;
    }    }
    return(n - nleft); /* return >= 0 */    return(n - nleft); /* return >= 0 */
Linea 42: Linea 53:
  
 ssize_t  /* Write "n" bytes to a descriptor */ ssize_t  /* Write "n" bytes to a descriptor */
-readn(int fd, void *ptr, size_t n) +writen(int fd, void *ptr, size_t n) {   
-{   +   size_t   nleft; 
- +   ssize_t  nwritten; 
 +    
 +   nleft = n; 
 +   while (nleft > 0) { 
 +     if((nwritten = write(fd, ptr, nleft)) < 0) { 
 +        if (nleft == n) return -1; /* error, return -1 */ 
 +        else break; /* error, return amount written so far */ 
 +     } else if (nwritten == 0) break;  
 +     nleft -= nwritten; 
 +     ptr   += nwritten; 
 +   } 
 +   return(n - nleft); /* return >= 0 */
 } }
 </code> </code>
  
informatica/sol/laboratorio20/esercitazionib/readnwriten.1588491950.txt.gz · Ultima modifica: 03/05/2020 alle 07:45 (5 anni fa) da Massimo Torquati

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki