, ), . , di_nlink I-node. ____________________ |- BSD - UNIX- University of California, Berkley. Berkley Software Distribution. . , 1992-95 - 145 - UNIX symbolic link - symlink. , I-node. , ; . 4.7. , @ . : lseek() - /: long offset, lseek(); ... /* /: * 0 . lseek * ( ). */ offset = lseek(fd, 0L, 1); /* ftell(fp) */ : lseek(fd, offset, 0); /* fseek(fp, offset, 0) */ lseek : - lseek(fd, offset, whence) / offset whence: 0 RWptr = offset; 1 RWptr += offset; 2 RWptr = _ + offset; whence : #include <stdio.h> 0 SEEK_SET 1 SEEK_CUR 2 SEEK_END - / -  , .. . / read()/write(). lseek() -  . - lseek() / RWptr   (long ). , - , lseek , - : long lseek(); - offset long ( !). - ( !), write() '\0' ; read() - "- 0 ". . - lseek() pipe FIFO-, 0 : /* */ int isapipe(int fd){ extern errno; return (lseek(fd, 0L, SEEK_CUR) < 0 && errno == ESPIPE); } "", fd - ""(pipe). . , 1992-95 - 146 - UNIX 4.8. ? int fd = creat("aFile", 0644); /* creat , rw-r--r-- */ write(fd, "begin", 5 ); lseek(fd, 1024L * 1000, 0); write(fd, "end", 3 ); close(fd); , ,  , . (, - 1/2 8 ). , . 1024003 . 1001 ( 1 )? UNIX - ! - : - - - . - ( ),  ( BSD, , ). - "" , "" . -   . : 0, 0 . ( 0 ) "begin". 5 ( - 1019 - !). lseek write 1000- "end". 1000- . "" 1..999. " ",   ( 0)! '\0'. "" . 1024003 , 2 ( , .. ). "" , - . , " " (, , - stat) - UNIX , ", ". 4.9. : FILE *fp; ... fp = open( "", "r" ); /* */ close(fp); /* */ : open() fopen(); close fclose, ( ) ! : : open, creat, close, read, write, lseek; stdio: fopen, fclose, fread, fwrite, fseek, getchar, putchar, printf, ... - fd - , - FILE *fp - . ( ), . , - . . , 1992-95 - 147 - UNIX 4.10. (/) ( ) , . , ( 1-10 ) char c; while( read(0, &c, 1)) ... ; /* 0 - */ : - - .  ( " UNIX"). . , ; , , , ( ). , stdio - " /" (standard input/output library). /lib/libc.a  (.. - , , ). #include <stdio.h> - , . stdio , - (.. ), ( ) , - - . -   (file descriptor), "" FILE.   (file pointer)|-. FILE : - fd ; - , ; - , - ; getc putc; - ( ) ( ); - (//+) . - |=; - ; FILE, stdin, stdout stderr 0, 1, 2 ( , , ). , () , , . ( malloc) - fopen(). 1 , - 512 ( BUFSIZ). int c; FILE *fp = ... ; c = getc(fp); ____________________ |- "" file , , - . |= feof(fp); , , - . . , 1992-95 - 148 - UNIX read- , getc . getc , ! - . , , ; . 1024 getc(), 1024 , 2 read - , - 512 . char c; FILE *fp = ... ; putc(c, fp); . , write . "- " : - ( BUFSIZ ). - (fclose exit |-|-). - fflush (. ). - - '\n' (. ). - - stdin (, gets), , stdout ( _IOLBF, ), - . , stdio ( ). s , c - , fp - FILE |=|=. , , . ,   read write, / . , - . , : #include <stdio.h> FILE *fp = fopen(char *name, char *rwmode); | V int fd = open (char *name, int irwmode); (fd < 0),  : fd = creat(char *name, int accessmode); fd . fopen() creat accessmode 0666 (rw- rw-rw-). ____________________ |-|- exit(); - . |=|= fd fp - . - : fp_output, fd_input ( fin, fout). . , 1992-95 - 149 - UNIX fopen open: rwmode irwmode ------------------------- "r" O_RDONLY "w" O_WRONLY|O_CREAT |O_TRUNC "r+" O_RDWR "w+" O_RDWR |O_CREAT |O_TRUNC "a" O_WRONLY|O_CREAT |O_APPEND "a+" O_RDWR |O_CREAT |O_APPEND r, r+ , , . fopen() ( ) , NULL: if((fp = fopen(name, rwmode)) == NULL){ ...... } , : printf(fmt,...)--->--,----fprintf(fp,fmt,...)->--* fp=stdout | fputs(s,fp)--------->--| puts(s)----------->-------putchar(c)-----,---->--| fp=stdout | fwrite(array,size,count,fp)->--| | putc(c,fp) ------------------* | |---<--write(fd,s,len)------------<---- |---->---read(fd,s,len)-* _flsbuf(c,fp) | | ! | | ! | | | ! V ungetc(c,fp) | - ! | | |(, ) ! | _filbuf(fp) | | | ! *--------->-----<-* | ! | ------------------* c=getc(fp) | rdcount=fread(array,size,count,fp)--<--| gets(s)-------<---------c=getchar()------,----<--| fp=stdout | | fgets(sbuf,buflen,fp)-<--| scanf(fmt,.../*-*/)--<-,--fscanf(fp,fmt,...)-* fp=stdin , : fclose(fp) ---> close(fd); - : fseek(fp,long_off,whence) ---> lseek(fd,long_off,whence); _flsbuf _filbuf - stdio, . fp : int fd = fileno(fp); FILE. , . , 1992-95 - 150 - UNIX open-, : int fd = open(name, O_RDONLY); /* creat() */ ... FILE *fp = fdopen(fd, "r"); ( , open-). fp, fd. , stdio. 4.11. ungetc(c,fp) "" . , . ( getc()) 1 .   (.. - ungetc- getc), , c, . while((c = getchar()) != '+' ); /* '+' */ ungetc(c ,stdin); /* ! */ c = getchar(); /* '+' */ 4.12. fputc, . : FILE *fp = ......; fputc( fp, '\n' ); ! int fputc( int c, FILE *fp ); ! putc( c, fp ); fputc, : #include <stdio.h> putNtimes( fp, c, n, f ) FILE *fp; int c; int n; int (*f)(); { while( n > 0 ){ (*f)( c, fp ); n--; }} putNtimes( fp, 'a', 3, fputc ); putNtimes( fp, 'a', 3, putc ); , , - - . , fgetc(fp) getc(fp). , putchar getchar #define putchar(c) putc((c), stdout) #define getchar() getc(stdin) . , 1992-95 - 151 - UNIX 4.13. printf stdio. - : FILE *fp; char bf[256]; fprintf(fp, fmt, ... ); printf( fmt, ... ); sprintf(bf, fmt, ... ); , fmt ( %-) - - ( putc) fp. - - fprintf fp stdout. , bf. sprintf '\0' - . fscanf(fp, fmt, /* - */...); scanf( fmt, ... ); sscanf(bf, fmt, ... ); fprintf fscanf - (  ). 4.14. ( ) . '\n'. , , 1 2 1 \n 2 \n =14 ! / (read/write pointer RWptr) ( ) \n - \r\n, ('\r') ('\n'), . MS DOS \r\n |-. \n, \n \r\n, , \n. , "": FILE *fp = fopen( , "rb" ); /* b - binary */ int fd = open ( , O_RDONLY | O_BINARY ); ____________________ |- : '\n' - '\012' (10) line feed '\r' - '\015' (13) carriage return '\t' - '\011' (9) tab '\b' - '\010' (8) backspace '\f' - '\014' (12) form feed '\a' - '\007' (7) audio bell (alert) '\0' - 0. null byte . , 1992-95 - 152 - UNIX MS DOS , - . , , \n \n, \r\n. ( !). : . : '\n' ,  . (, ) '\n', 1. 4.15. 4 . (: 256 . long). 4.16. getchar() getc(fp) - int char? : getchar() , EOF (end of file), (-1). ASCII, getchar() ( 0...255), . ( 1 ). : ... while((ch = getchar()) != EOF ){ putchar(ch); ... } - ch unsigned char. ch 0...255 (-1). getchar() , unsigned char 255. - (-1) int 255. , , .. 255 (255 != -1). - ch signed char. EOF ch signed int (7- ). getchar (-1), ch char: 255; EOF 255 int (-1). , . , 255, (-1) . , 255, ( - - ). - ch int unsigned int ( 8 ). . , UNIX . ; EOF , , - ( - ). MS DOS (EOF) CTRL/Z. , - CTRL/Z, "" ! , : scanf, fscanf, fgetc, getc, getchar EOF, read - 0, gets, fgets - NULL. . , 1992-95 - 153 - UNIX 4.17. , . gets(s); fgets(s,slen,fp); ? : gets() ( '\n') fp==stdin. , , - ( - ). -   ( - !), () . fgets() : - , slen , s, - " ". fgets . fgets, gets, '\n' , , "" - '\0', "\n\0". char buffer[512]; FILE *fp = ... ; int len; ... while(fgets(buffer, sizeof buffer, fp)){ if((len = strlen(buffer)) && buffer[len-1] == '\n') /* @ */ buffer[--len] = '\0'; printf("%s\n", buffer); } len - . , '@', printf , '\n' - buffer "%s\n". ( ), gets fgets NULL. , NULL, EOF. - , - , - . : #include <stdio.h> #include <string.h> char buffer[512]; FILE *fp = ... ; ... while(fgets(buffer, sizeof buffer, fp) != NULL){ char *sptr; if(sptr = strchr(buffer, '\n')) *sptr = '\0'; printf("%s\n", buffer); } 4.18. puts(s); fputs(s,fp); ? : puts s stdout. puts s, - - '\n'. fputs . : fputs(s, fp) char *s; FILE *fp; { while(*s) putc(*s++, fp); } puts(s) char *s; { fputs(s, stdout); putchar('\n'); } . , 1992-95 - 154 - UNIX 4.19. : #include <stdio.h> main() { int fp; int i; char str[20]; fp = fopen(""); fgets(stdin, str, sizeof str); for( i = 0; i < 40; i++ ); fputs(fp, ", :%s",str ); fclose(""); } : . 4.20. , . 4.21. , n- . main(). 4.22. slice - +   ,  ( ). #include <stdio.h> #include <ctype.h> long line, count, nline, ncount; /* */ char buf[512]; void main(int argc, char **argv){ char c; FILE *fp; argc--; argv++; /* */ while((c = **argv) == '-' || c == '+'){ long atol(), val; char *s = &(*argv)[1]; if( isdigit(*s)){ val = atol(s); if(c == '-') nline = val; else ncount = val; } else fprintf(stderr," %s\n", s-1); argc--; ++argv; } if( !*argv ) fp = stdin; else if((fp = fopen(*argv, "r")) == NULL){ fprintf(stderr, " %s\n", *argv); exit(1); } for(line=1, count=0; fgets(buf, sizeof buf, fp); line++){ if(line >= nline){ fputs(buf, stdout); count++; } if(ncount && count == ncount) break; } . , 1992-95 - 155 - UNIX fclose(fp); /*  */ } /* End_Of_File */ 4.23. , n . 4.24. , n . 4.25. , 2 : , - . , , . 4.26. , , . 4.27. . - , : 1. . 2. . 3. . 4. . 5. . . - EOF (.. CTRL/D), '.' . . : --more-- _ ( ) . 'q' . , , - - . . - ( <ENTER>, , . , , [] . [oldfile.txt]: _ (. " "), - . : ( . UNIX). system("ls -x"); |- FILE *fp = popen("ls *.c", "r"); ... fgets(...,fp); ... // , EOF pclose(fp); ( .c ). 4.28. n- ; m-. , ( ) . . , 1992-95 - 156 - UNIX 4.29. , -8, - . 256 : c_new=TABLE[c_old]; - strchr(). . 4.30. , ( , ). - ( ). #include <fcntl.h> #include <stdio.h> #define min(a,b) (((a) < (b)) ? (a) : (b)) #define KB 1024 /* */ #define PORTION (20L* KB) /* < 32768 */ long ONEFILESIZE = (300L* KB); extern char *strrchr(char *, char); extern long atol (char *); extern errno; /* */ char buf[PORTION]; /* */ void main (int ac, char *av[]) { char name[128], *s, *prog = av[0]; int cnt=0, done=0, fdin, fdout; /* M_UNIX * UNIX */ #ifndef M_UNIX /* .. MS DOS */ extern int _fmode; _fmode = O_BINARY; /* */ #endif if(av[1] && *av[1] == '-'){ /* */ ONEFILESIZE = atol(av[1]+1) * KB; av++; ac--; } if (ac < 2){ fprintf(stderr, "Usage: %s [-size] file\n", prog); exit(1); } if ((fdin = open (av[1], O_RDONLY)) < 0) { fprintf (stderr, "Cannot read %s\n", av[1]); exit (2); } if ((s = strrchr (av[1], '.'))!= NULL) *s = '\0'; do { unsigned long sent; sprintf (name, "%s.%d", av[1], ++cnt); if ((fdout = creat (name, 0644)) < 0) { fprintf (stderr, "Cannot create %s\n", name); exit (3); } sent = 0L; /* */ for(;;){ unsigned isRead, /* read- */ need = min(ONEFILESIZE - sent, PORTION); if( need == 0 ) break; sent += (isRead = read (fdin, buf, need)); errno = 0; if (write (fdout, buf, isRead) != isRead && errno){ perror("write"); exit(4); } else if (isRead < need){ done++; break; } } if(close (fdout) < 0){ perror(" "); exit(5); } printf("%s\t%lu \n", name, sent); } while( !done ); exit(0); } . , 1992-95 - 157 - UNIX 4.31. , . cat : , , . "-". #include <fcntl.h> #include <stdio.h> void main (int ac, char **av){ int i, err = 0; FILE *fpin, *fpout; if (ac < 3) { fprintf(stderr,"Usage: %s from... to\n", av[0]); exit(1); } fpout = strcmp(av[ac-1], "-") ? /* "-" */ fopen (av[ac-1], "wb") : stdout; for (i = 1; i < ac-1; i++) { register int c; fprintf (stderr, "%s\n", av[i]); if ((fpin = fopen (av[i], "rb")) == NULL) { fprintf (stderr, "Cannot read %s\n", av[i]); err++; continue; } while ((c = getc (fpin)) != EOF) putc (c, fpout); fclose (fpin); } fclose (fpout); exit (err); } MS DOS UNIX. UNIX b "rb", "wb". read #ifdef M_UNIX # define O_BINARY 0 #endif int fdin = open( av[1], O_RDONLY | O_BINARY); 4.32. , - - ? , ; ? ? - , ? : . fopen, freopen, dup2, stat. : 1 ( ) #include <stdio.h> ... freopen( "_", "r", stdin ); 2 ( ) #include <fcntl.h> int fd; ... fd = open( "_", O_RDONLY ); dup2 ( fd, 0 ); /* 0 - */ close( fd ); /* fd - , */ . , 1992-95 - 158 - UNIX 3 ( ) #include <fcntl.h> int fd; ... fd = open( "_", O_RDONLY )