1 Îļþ²Ù×÷Ïà¹Øº¯Êý
1.1 open
±»²Ù×÷Îļþ±¾Éí±ØÐë¾ß±¸ÏàÓ¦¶ÁдȨÏÞ£¬Èç¹ûÎļþ±¾Éí²»¾ß±¸¶ÁдȨÏÞ£¬ÄÇô¾ÍÎÞ·¨¶ÔÎļþ½øÐжÁд²Ù×÷
Í·Îļþ£º #include <sys/types.h> /usr/include/sys/types.h
#include<sys/stat.h>
#include <fcntl.h> /usr/include/fcntl.h
º¯ÊýÔÐÍ£º
int open(const char*pathname, int flags, mode_t mode);
×÷Ó㺴ò¿ª/´´½¨Ò»¸öÎļþ£¬²¢ÇÒ·µ»ØÎļþÃèÊö·û
ÐβÎÁÐ±í£º
pathname£ºÐèÒª´ò¿ªµÄ´ø·¾¶µÄÎļþ
flags £º²Ù×÷·½Ê½
O_RDONLY£ºÖ»¶Á
O_WRONLY£ºÖ»Ð´
O_RDWR£º¶Áд
O_CREAT£º´´½¨£¬Èç¹ûÎļþ´æÔÚ£¬¾Í²»´´½¨£¬Èç¹ûÎļþ²»´æÔÚ£¬Ôò»á´´½¨Ò»¸öÎļþ
O_TRUNC£ºÇå¿Õд£¬Ö»ÄÜÔÚÓÐдȨÏÞϲſÉÒÔÉúЧ£¬Ö»»áÔÚµÚÒ»´Î´ò¿ªÎļþµÄʱºòÇå¿Õ£¬ºóÃæдµÄʱºò£¬²»»áÔÙÇå¿Õ¡£
O_EXCL£º¼ì²âÎļþÊÇ·ñ´æÔÚ
O_APPEND£º×·¼Óд£¬Ê¹ÓÃÕâ¸ö²Ù×÷·½Ê½£¬Ö´ÐÐд²Ù×÷£¬»áÔÚÎļþĩβдÈ룬ÎļþÆ«ÒÆÖ¸Õë»áÆ«ÒƵ½×îºó¡£Ð´Ö®Ç°£¬ÎļþÆ«ÒÆÖ¸Õë²»»á·¢ÉúÆ«ÒÆ
mode£ºÈ¨ÏÞ£¬Ö»ÓÐʹÓÃÁËO_CREAT²Ù×÷·½Ê½²¢ÇÒ´´½¨ÐÂÎļþ²Å»áʹÓõ½Õâ¸öÐβΣ¬Õâ¸öÐβλá×÷ΪȨÏÞÖµ£¬¸³Öµ¸øд´½¨µÄÎļþ¡£
·µ»ØÖµ£º
ʧ°Ü£º-1
³É¹¦£º·µ»ØÒ»¸öδ±»Ê¹ÓõÄ×îСµÄÎļþÃèÊö·û
ÑÚÂ룺ÃüÁî umask
×÷ÓãºÉèÖÃÑÚÂ룬Ϊ1µÄλ±íʾÔÚд´½¨¸³ÖµÈ¨ÏÞµÄʱºò£¬¶ÔӦλµÄȨÏÞ²»»á±»¸³Óè
Àý×Ó£ºumask ---> 0002 µ±¸³ÓèȨÏÞʱ£¬µ¹ÊýµÚ¶þ¸öȨÏÞ²»»á±»¸³Óè
1.2 write
±»²Ù×÷Îļþ±¾Éí±ØÐë¾ß±¸ÏàӦдȨÏÞ£¬Èç¹ûÎļþ±¾Éí²»¾ß±¸Ð´È¨ÏÞ£¬ÄÇô¾ÍÎÞ·¨¶ÔÎļþ½øÐÐд²Ù×÷
Í·Îļþ£º#include<unistd.h>
º¯ÊýÔÐÍ£ºssize_twrite(int fd, const void *buf, size_t count);
×÷Ó㺴Óbuf»º´æÇøÖжÁÈ¡count¸ö×Ö½ÚÊý¾Ý£¬Ð´Èëµ½fdÎļþÃèÊö·ûÖÐ
ÐβÎÁÐ±í£º
fd£ºÒªÐ´Èëµ½µÄÎļþµÄÎļþÃèÊö·û
buf£º´æ´¢ÓÃÀ´Ð´ÈëµÄÊý¾Ý
count£ºÒªÐ´È뼸¸ö×Ö½ÚµÄÊý¾Ý
·µ»ØÖµ£º
³É¹¦£ºÐ´ÈëµÄ×Ö½ÚÊý
ʧ°Ü£º-1
×¢Ò⣺Èç¹ûopenµÄʱºòÓÃÁËO_TRUNC²Ù×÷·½Ê½£¬»á°ÑÎļþ²Á³ýÖ®ºóÔÙд£¬Èç¹ûÊÇO_APPEND²Ù×÷·½Ê½£¬ÔòÊÇÔÚÎļþĩβ¼ÌÐøÍùºóÃæд£¬Á½Õ߶¼Ã»ÓУ¬¾ÍÊÇĬÈϵĸ²¸Çд£¬´ÓÍ·¿ªÊ¼£¬»á±»Ö®Ç°µÄÊý¾ÝÍùÏÂÒ»Ö±¸²¸ÇÏÂÈ¥
1.3 read
±»²Ù×÷Îļþ±¾Éí±ØÐë¾ß±¸ÏàÓ¦¶ÁȨÏÞ£¬Èç¹ûÎļþ±¾Éí²»¾ß±¸¶ÁȨÏÞ£¬ÄÇô¾ÍÎÞ·¨¶ÔÎļþ½øÐжÁ²Ù×÷
Í·Îļþ£º#include<unistd.h>
º¯ÊýÔÐÍ£ºssize_tread(int fd, void *buf, size_t count);
×÷Ó㺴ÓfdÎļþÃèÊö·û¶ÁÈ¡count¸ö×Ö½ÚµÄÊý¾Ý£¬´æ´¢µ½bufÖÐ
ÐβÎÁÐ±í£º
fd£ºÐèÒª²Ù×÷µÄÎļþµÄÎļþÃèÊö·û
buf£º´æ´¢Êý¾ÝµÄ»º´æÇø
count£ºÐèÒª¶ÁÈ¡µÄ×Ö½ÚÊý
·µ»ØÖµ£º
³É¹¦£º¶ÁÈ¡µ½µÄ×Ö½ÚÊý
ʧ°Ü£º-1
×¢Ò⣺¶ÁÈ¡µÄʱºò£¬²»Òª°ÑÕû¸ö»º´æÇøÈ«¶¼´æÂúÊý¾Ý£¬ÐèÒª¸ø»º´æÇøÁôÏÂÒ»¶¨µÄ¿Õ¼äÀ´Ìí¼ÓһЩ½áÊø·ûÖ®ÀàµÄÊý¾Ý
1.4 close
Í·Îļþ£º#include<unistd.h>
º¯ÊýÔÐÍ£ºint close(int fd);
×÷Ó㺹رÕfdÎļþÃèÊö·û
ÐβÎÁÐ±í£º
fd£ºÐèÒª¹Ø±ÕµÄÎļþÃèÊö·û
·µ»ØÖµ£º
³É¹¦£º0
ʧ°Ü£º-1
×¢Ò⣺һ°ãÔÚ²»ÐèҪʹÓÃÕâ¸öÎļþÃèÊö·ûÖ®ºó£¬¾Í¿ÉÒÔ°ÑÎļþÃèÊö·û¹Ø±Õµô£¬È»ºóÄÚºËÎļþ¹ÜÀí×ÜÏß¿ÉÒÔÔٴΰÑÕâ¸öÎļþÃèÊö·û·ÖÅä¸ø±ðµÄ±»´ò¿ªµÄÎļþ
1.5 lseek
Í·Îļþ£º #include <sys/types.h>
#include<unistd.h>
º¯ÊýÔÐÍ£ºoff_t lseek(int fd, off_t offset, intwhence);
×÷Ó㺲Ù×÷fdÎļþÃèÊö·ûµÄÎļþÆ«ÒÆÖ¸Õ룬ÒÔwhenceΪ»ù×¼µã£¬Æ«ÒÆoffset¸ö×Ö½ÚµÄλÖÃ
ÐβÎÁÐ±í£º
fd£ºÒª²Ù×÷ÎļþÃèÊö·û
offset£ºÒªÆ«ÒƵľàÀ룬ÒÔ×Ö½ÚΪµ¥Î»£¬ÒÔÕý¸ºÀ´Çø·Ö·½Ïò£¬¸ºÖµÎªÏò×óÆ«ÒÆ£¬ÕýֵΪÏòÓÒÆ«ÒÆ
whence£ºÆ«ÒƵĻù×¼µã
SEEK_SET£ºÒÔÎļþ¿ªÍ·Îª»ù×¼µã
SEEK_CUR£ºÒÔÎļþµ±Ç°Î»ÖÃΪ»ù×¼µã
SEEK_END£ºÒÔÎļþĩβΪ»ù×¼µã
·µ»ØÖµ£º
³É¹¦£ºÆ«ÒƺóµÄλÖÃÓëÎļþ¿ªÍ·µÄ¾àÀ룬ÒÔ×Ö½ÚΪµ¥Î»£¨¸ù¾Ý·µ»ØÖµ£¬¿ÉÒÔʵÏÖ¼ì²âÎļþ´óСµÄ¹¦ÄÜ£©
ʧ°Ü£º-1
×¢Ò⣺¶Ôͬһ¸öÎļþÃèÊö·û½øÐвÙ×÷µÄʱºò£¬ÎļþÆ«ÒÆÖ¸ÕëÆ«ÒƵ½Ê²Ã´Î»Ö㬺óÐøµÄ²Ù×÷¾Í´ÓʲôλÖÿªÊ¼£¬ÒòΪһ¸öÎļþÃèÊö·û£¬Ö»ÓÐÒ»¸öÎļþÆ«ÒÆÖ¸Õë¡£
Èç¹ûÊÇͬһ¸öÎļþ²»Í¬µÄÎļþÃèÊö·û£¬ÄÇô±Ë´ËÖ®¼äÎļþÃèÊö·û»¥²»Ó°Ïì¡£