POSIX API ×îÖØÒªµÄÒ»¸ö³éÏó¸ÅÄî¾ÍÊÇÎļþ¡£¾¡¹Ü¼¸ºõËùÓеIJÙ×÷ϵͳ¶¼½«ÎļþÓÃÓÚÓÀ¾ÃÐÔ´æ´¢Æ÷£¬µ«ËùÓÐ Unix °æ±¾Í¨¹ýÎļþ³éÏó¸ÅÄîÌṩ¶Ô´ó¶àÊýϵͳ×ÊÔ´µÄ·ÃÎÊ¡£
¸ü¾ßÌåµØ˵£¬ÕâÒâζ×Å linux ʹÓÃÏàͬµÄÒ»×éϵͳµ÷ÓÃÀ´Ìṩ¶ÔÉ豸£¨ÀýÈçÈíÅ̺ʹŴøÉ豸£©¡¢ÍøÂç×ÊÔ´£¨×î³£¼ûµÄÊÇ TCP/IP Á¬½Ó£©¡¢ÏµÍ³Öնˣ¬ÉõÖÁÄÚºË״̬ÐÅÏ¢µÄ·ÃÎÊ¡£¸ÐлÎÞËù²»ÔÚµÄϵͳµ÷Óã¬æµÊìµØʹÓÃÓëÎļþÏà¹ØµÄµ÷ÓöÔÓÚÿ¸ö Linux ³ÌÐòÔ±À´Ëµ¶¼ºÜÖØÒª¡£ÈÃÎÒÃÇ×Ðϸ²é¿´Ò»ÏÂÎļþ API ±³ºóµÄһЩ»ù±¾¸ÅÄ²¢ÃèÊö×îÖØÒªµÄÎļþÏà¹Øϵͳµ÷Óá£
linux ÌṩÐí¶à²»Í¬ÖÖÀàµÄÎļþ¡£×î³£¼ûµÄÀàÐ;ͼò³ÆΪ³£¹æÎļþ£¬Ëü´æ´¢´óÁ¿ÓÃÓÚÒÔºó·ÃÎʵÄÐÅÏ¢¡£ÄúËùʹÓõľø´ó²¿·ÖÎļþ -- ÀýÈç¿ÉÖ´ÐÐÎļþ£¨Èç /bin/vi£©¡¢Êý¾ÝÎļþ£¨Èç /etc/passwd£©ºÍϵͳ¶þ½øÖÆÎļþ£¨Èç /lib/libc.so.6£©-- ¶¼Êdz£¹æÎļþ¡£ËüÃÇͨ³£×¤ÁôÔÚ´ÅÅÌÉϵÄij´¦£¬µ«ÎÒÃÇÉÔºó»á·¢ÏÖ£¬²¢²»Ò»¶¨¶¼ÊÇÕâÖÖÇé¿ö¡£
ÁíÒ»ÖÖÎļþÀàÐÍÊÇĿ¼£¬Ëü°üº¬ÁËÒ»¸öÆäËüÎļþ¼°ÆäλÖõÄÁÐ±í¡£Ê¹Óà ls ÃüÁîÁгöĿ¼ÖеÄÎļþʱ£¬Ëü´ò¿ª¸ÃĿ¼µÄÎļþ£¬²¢´òÓ¡³öËüËù°üº¬µÄËùÓÐÎļþµÄÐÅÏ¢¡£
ÆäËüÎļþÀàÐÍ°üÀ¨¿éÉ豸£¨±íʾÎļþϵͳ¸ßËÙ»º´æµÄÉ豸£¬ÀýÈçÓ²ÅÌÇý¶¯Æ÷£©¡¢×Ö·ûÉ豸£¨±íʾ·Ç¸ßËÙ»º´æµÄÉ豸£¬ÀýÈç´Å´øÇý¶¯Æ÷¡¢Êó±êºÍϵͳÖնˣ©¡¢¹ÜµÀºÍÌ×½Ó×Ö£¨ÔÊÐí½ø³ÌÏ໥֮¼ä¶Ô»°£©£¬ÒÔ¼°·ûºÅÁ´½Ó£¨ÔÊÐíÎļþÔÚĿ¼²ã´Î½á¹¹ÖÐÓжà¸öÃû³Æ£©¡£
´ó¶àÊýÎļþ¶¼ÓÐÒ»¸ö»ò¶à¸öÒýÓÃËüÃǵķûºÅÃû¡£ÕâЩ·ûºÅÃûÊÇÒ»×éÓÉ / ×Ö·û¶¨½çµÄ×Ö·û´®£¬²¢ÏòÄں˱êʶÎļþ¡£ËüÃÇÊÇ linux Óû§ËùÊìϤµÄ·¾¶Ãû£»ÀýÈ磬·¾¶Ãû /home/ewt/article ÒýÓõÄÊÇÎÒÊÖÌáµçÄÔÖаüº¬ÕâƪÎÄÕÂÎı¾µÄÎļþ¡£Ã»ÓÐÁ½¸öÎļþ¿ÉÒÔ¹²ÏíÏàͬµÄÃû³Æ£¨µ«µ¥Ò»Îļþ¿ÉÒÔÓжà¸öÃû³Æ£©£¬Òò´Ë·¾¶ÃûΨһµØ±êʶµ¥Ò»Îļþ¡£
½ø³Ì¿ÉÒÔ·ÃÎʵÄÿ¸öÎļþ¶¼ÓÉÒ»¸öСµÄ·Ç¸ºÕûÊý±êʶ£¬³ÆΪ¡°ÎļþÃèÊö·û¡±¡£ÎļþÃèÊö·ûÓÉ´ò¿ªÎļþµÄϵͳµ÷Óô´½¨£¬²¢ÓÉ´Óµ±Ç°½ø³Ì´´½¨µÄÐÂ×Ó½ø³Ì¼Ì³Ð¡£¾ÍÊÇ˵£¬µ±½ø³ÌÆô¶¯ÁËÒ»¸öгÌÐòʱ£¬Ôʼ½ø³ÌµÄ´ò¿ªÎļþͨ³£ÊÇÓÉгÌÐò¼Ì³ÐµÄ¡£
°´ÕÕÔ¼¶¨£¬´ó¶àÊý³ÌÐò±£ÁôÇ°Èý¸öÎļþÃèÊö·û£¨0¡¢1 ºÍ 2£©ÓÃÓÚÌØÊâÄ¿µÄ -- ·ÃÎÊËùνµÄ±ê×¼Êä³ö¡¢±ê×¼Êä³öºÍ±ê×¼´íÎóÁ÷¡£ÎļþÃèÊö·û 0 ÊDZê×¼ÊäÈ룬ÕâÀïÐí¶à³ÌÐò¶¼½«´ÓÍⲿÊÀ½ç½ÓÊÕÊäÈë¡£ÎļþÃèÊö·û 1 ÊDZê×¼Êä³ö¡£´ó¶àÊý³ÌÐòÔÚÕâÀïÏÔʾÕý³£µÄÊä³ö¡£¶ÔÓÚÓë´íÎóÇé¿öÏà¹ØµÄÊä³ö£¬Ê¹ÓÃÎļþÃèÊö·û 2£¨±ê×¼´íÎ󣩡£
ÈκÎÏ°¹ßʹÓà linux shell µÄÈ˶¼Ôø¿´µ½¹ý±ê×¼ÊäÈë¡¢Êä³öºÍ´íÎóÎļþÃèÊö·ûµÄʹÓá£Í¨³££¬shell ÔËÐÐÃüÁîʱ´øÎļþÃèÊö·û 0¡¢1 ºÍ 2£¬¶¼ÊÇÖ¸ shell µÄÖնˡ£µ±Ê¹Óà > ×Ö·ûָʾ shell ½«Ò»¸ö³ÌÐòµÄÊä³ö·¢Ë͸øÁíÒ»¸ö³ÌÐòʱ£¬shell ÔÚµ÷ÓÃгÌÐò֮ǰ´ò¿ª¸ÃÎļþ×÷ΪÎļþÃèÊö·û 1¡£Õ⽫µ¼Ö³ÌÐò½«ËüµÄÊä³ö·¢Ë͸øÖ¸¶¨µÄÎļþ¶ø²»ÊÇÓû§Öնˣ»ÆäÃî´¦ÊÇ£¬¶ÔÓÚ³ÌÐò±¾Éí£¬ÕâÊÇ͸Ã÷µÄ£¡
ÓëÖ®ÀàËÆ£¬"<" ×Ö·ûָʾ shell ʹÓÃÌض¨µÄÎļþ×÷ΪÎļþÃèÊö·û 0¡£ÕâÑù¾ÍÇ¿ÆȳÌÐò´Ó¸ÃÎļþÖжÁÈ¡ËüµÄÊäÈ룻ÕâÁ½ÖÖÇé¿öÏ£¬ÈκÎÀ´×Ô³ÌÐòµÄ´íÎóÈÔ½«³öÏÖÔÚÖÕ¶ËÉÏ£¬ÈçͬËüÃÇÔÚÎļþÃèÊö·û 2 µÄÇé¿öÏ·¢Ë͸ø±ê×¼´íÎóÒ»Ñù¡££¨ÔÚ "bash" shell ÖУ¬¿ÉÒÔʹÓà 2> ¶ø²»ÊÇ > ½«±ê×¼´íÎóÖض¨Ïò£©¡£ÕâÖÖÀàÐ͵ÄÎļþÖض¨ÏòÊÇ linux ÃüÁîÐÐ×îÇ¿´óµÄÌØÐÔÖ®Ò»¡£
ʹÓÃÈκÎÓëÎļþÏà¹ØµÄϵͳµ÷ÓÃ֮ǰ£¬³ÌÐòÓ¦¸Ã°üÀ¨
#include
#include
Ê×ÏÈ£¬ÈÃÎÒÃÇÁ˽âÈçºÎ¶ÁдÎļþ¡£Æ¾Ö±¾õ¾Í¿ÉÒÔÖªµÀ£¬read() ºÍ write() ϵͳµ÷ÓÃÊÇÖ´ÐÐÕâЩ²Ù×÷µÄ×î³£Ó÷½·¨¡£ÕâÁ½ÖÖϵͳµ÷Óý«ÓÐÈý¸ö×Ô±äÁ¿£ºÒª·ÃÎʵÄÎļþÃèÊö·û¡¢Ö¸ÏòÒª¶ÁдµÄÐÅÏ¢µÄÖ¸ÕëÒÔ¼°Ó¦¸Ã¶ÁдµÄ×Ö·ûÊý¡£·µ»Ø³É¹¦¶ÁдµÄ×Ö·ûÊý¡£Çåµ¥ 1 ˵Ã÷ÁËÒ»¸ö¼òµ¥µÄ³ÌÐò£¬Ëü´Ó±ê×¼ÊäÈ루ÎļþÃèÊö·û 0£©ÖжÁÈ¡Ò»ÐУ¬²¢½«ËüдÈë±ê×¼Êä³ö£¨ÎļþÃèÊö·û 1£©£º
Çåµ¥ 1£º
void main(void) {
char buf[100];
int num;
num = read(0, buf, sizeof(buf));
write(1, "I got: ", 7); /* Length of "I got: " is 7! */
write(1, buf, num);
}
¹ØÓÚÕâ¸ö´¦ÀíÓÐÁ½¸öÖµµÃ×¢ÒâµÄÎÊÌâ¡£Ê×ÏÈ£¬ÎÒÃÇÒªÇó read() ·µ»Ø 100 ¸ö×Ö·û£¬µ«Èç¹ûÎÒÃÇÔËÐÐÕâ¸ö³ÌÐò£¬Ö»ÓÐÔÚÓû§°´ÏÂÁË "enter" ¼üÒÔºó²ÅÄÜ»ñµÃÊäÈë¡£Ðí¶àÎļþ²Ù×÷¶¼¸ù¾Ý×î¼ÑЧ¹û¹¤×÷£ºËüÃdz¢ÊÔ·µ»Ø³ÌÐòÒªÇóµÄËùÓÐÐÅÏ¢£¬µ«Ö»Óв¿·ÖÄܹ»³É¹¦¡£È±Ê¡Çé¿öÏ£¬ÖÕ¶ËÅäÖóÉÒ»µ©´æÔÚ " " »òÐÂÐзû£¨Í¨¹ý°´ "enter" ¼ü²úÉú£©Ê±£¬¾Í´Ó read() µ÷Ó÷µ»Ø¡£Õâʵ¼ÊÉϷdz£·½±ã£¬ÒòΪ´ó¶àÊýÓû§¶¼Ï£Íû³ÌÐòÎÞÂÛÈçºÎ¶¼ÊÇÃæÏòÐеġ£µ«³£¹æÊý¾ÝÎļþ²¢·ÇÈç´Ë£¬Èç¹ûÒÀ¿¿Ëü¾Í¿ÉÄܲúÉú²»¿ÉÔ¤ÁϵĽá¹û¡£
ÁíÒ»¸öҪעÒâµÄÎÊÌâÊÇÎÒÃDz»±ØÔÚÏÔʾÊä³öºóдһ¸ö ¡£read() µ÷ÓøøÁËÎÒÃÇÀ´×ÔÓû§µÄ £¬Ö»½«ÄǸö ͨ¹ý write() д»Ø±ê×¼Êä³ö¡£Èç¹ûÄúÏ£ÍûÔÚûÓÐÐÂÐзûµÄÇé¿öÏ¿´µ½·¢ÉúµÄʼþ£¬³¢ÊÔ½«×îºóÒ»ÐиÄΪ
write(1, buf, num - 1);
ÓйØÕâ¸ö¼òµ¥Ê¾ÀýµÄ×îºóÒ»µã£ºbuf ¾ø¶Ô²»°üº¬Êµ¼ÊµÄ C ×Ö·û´®¡£C ×Ö·û´®Óɱê¼Ç×Ö·û´®½áÊøµÄµ¥Ò» ×Ö·ûÖÕÖ¹¡£ÒòΪ read() ²»½« Ìí¼Óµ½»º³åÇøµÄ½á⣬ÔÚ read() ÉÏʹÓà strlen()£¨»òÈκÎÆäËü C ×Ö·û´®º¯Êý£©½«¿ÉÄÜÖý³É´ó´í£¡ÕâÖÖÐÐΪ¿ÉÒÔÈà read() ºÍ write() ¶Ô°üÀ¨ ×Ö·ûµÄÊý¾Ý´¦Àí£¬¶øÕâ¶ÔÓÚÒ»°ã×Ö·û´®º¯ÊýÀ´ËµÊDz»¿ÉÄܵġ£
read() ºÍ write() ϵͳµ÷ÓÿÉÒÔ¶Ô¾ø´ó¶àÊýÎļþÆð×÷Óᣵ«ËüÃDz»¶ÔĿ¼Æð×÷Óã¬Ä¿Â¼Ó¦¸Ãͨ¹ýÌØÊ⺯Êý£¨ÀýÈç readdir()£©À´·ÃÎÊ¡£ÁíÍ⣬read() ºÍ write() ¶ÔÓÚijЩÀàÐ͵ÄÌ×½Ó×ÖÒ²²»Æð×÷Óá£
ijЩÎļþ£¬ÀýÈç³£¹æÎļþºÍ¿éÉ豸Îļþ£¬Ê¹ÓÃÎļþÖ¸ÕëµÄ¸ÅÄî¡£ËüÖ¸¶¨ÔÚÎļþÖУ¬ÏÂÒ»¸ö read() µ÷ÓôÓÄÄÀï¶ÁÈ¡£¬ÏÂÒ»¸ö write() µ÷ÓôÓÄÄÀïдÈë¡£read() »ò write() ºó£¬ÎļþÖ¸ÕëËæ×ÅÒÑ´¦ÀíµÄ×Ö·ûÊý£¨ÔÚÄÚ²¿£¬Í¨¹ýÄںˣ©Ôö¼Ó¡£ÕâÑù£¬Ê¹Óõ¥Ò»Ñ»·¾Í¿ÉÒÔ·½±ãµØ¶ÁÈ¡ÎļþÖеÄËùÓÐÊý¾Ý¡£Çåµ¥ 2 ¾ÍÊÇʾÀý£º
Çåµ¥ 2£º
char buffer[1024];
while ((num = read(0, buffer, 1024))) {
printf("got some data ");
}
Õâ¸öÑ»·½«¶ÁÈ¡±ê×¼ÊäÈëÉϵÄËùÓÐÊý¾Ý£¬×Ô¶¯ÔÚÿ´Î¶ÁÈ¡ºóÔö¼ÓÄں˵ÄÄÚ²¿ÎļþÖ¸Õë¡£µ±ÎļþÖ¸Õë´¦ÓÚÎļþ½áβʱ£¬read() ½«·µ»Ø 0 ²¢Í˳öÑ»·¡£Ä³Ð©Îļþ£¨ÀýÈç×Ö·ûÉ豸 -- Öն˾ÍÊǺܺõÄÒ»Àý£©±¾ÉíûÓÐÎļþÖ¸Õ룬ËùÒÔ¶ÔÓÚÕâÒ»µã£¬¸Ã³ÌÐò½«¼ÌÐøÔËÐУ¬Ö±µ½Óû§ÌṩÎļþ½áÊø±ê¼Ç£¨Í¨¹ý°´ "Ctrl-D"£©ÎªÖ¹¡£
µ½ÏÖÔÚΪֹ£¬ÎÒÃÇÒѾ֪µÀÈçºÎ¶ÁдÎļþÁË£¬ÏÂÒ»²½ÒªÑ§Ï°ÈçºÎ´ò¿ªÒ»¸öÐÂÎļþ¡£´ò¿ª²»Í¬ÀàÐ͵ÄÎļþÓв»Í¬·½·¨£»ÎÒÃǽ«ÔÚÕâÀïÌÖÂ۵ķ½·¨ÊÇͨ¹ý·¾¶Ãû´ò¿ªÔÚÎļþϵͳÖбíʾµÄÎļþ£»°üÀ¨³£¹æÎļþ¡¢Ä¿Â¼¡¢É豸ÎļþºÍÖ¸¶¨µÄ¹ÜµÀ¡£Ä³Ð©Ì×½Ó×ÖÎļþÓз¾¶Ãû£¬ÄÇЩ±ØÐëͨ¹ýÌæ´ú·½·¨´ò¿ª¡£
Ʋ¿ª·ÅÆúȨÀûµÄ£¬open() ϵͳµ÷ÓÿÉÒÔÈóÌÐò·ÃÎÊ´ó¶àÊýϵͳÎļþ¡£open() ÊǸö²»Ñ°³£µÄϵͳµ÷Óã¬ÒòΪËü»ñÈ¡Á½¸ö»òÕßÈý¸ö×Ô±äÁ¿£º
int open(const char *
pathname,
int flags);
»òÕߣ¬
int open(const char *
pathname,
int flags,
int perm);
µÚÒ»ÖÖÐÎʽ¸üÆÕ±éһЩ£»Ëü´ò¿ªÒ»¸öÒÑ´æÔÚµÄÎļþ¡£µÚ¶þÖÖ¸ñʽӦ¸ÃÔÚÐèÒª´´½¨ÎļþʱʹÓᣵÚÈý¸ö×Ô±äÁ¿Ö¸¶¨Ó¦¸Ã¸øÓèÐÂÎļþµÄ·ÃÎÊȨÏÞ¡£
open() µÄµÚÒ»¸ö²ÎÊýÊÇÒÔÕý³£ C ×Ö·û´®±íʾµÄȫ·¾¶Ãû£¨¼´ÒÔ ÖÕÖ¹£©¡£µÚ¶þ¸ö²ÎÊýÖ¸¶¨ÎļþÓ¦¸ÃÈçºÎ´ò¿ª£¬²¢°üÀ¨Âß¼¡°Ó롱²Ù×÷µÄÒ»¸ö»ò¶à¸öÒÔϱêÖ¾£º
O_RDONLY£ºÎļþ¿ÉÒÔÖ»¶Á
O_RDWR£ºÎļþ¿ÉÒÔ¶Áд
O_APPEND£ºÎļþ¿ÉÒÔ¶Á»ò¸½¼Ó
O_CREAT£ºÈç¹ûÎļþ»¹²»´æÔÚÔòÓ¦¸Ã´´½¨
O_EXCL£ºÈç¹ûÎļþÒÑ´æÔÚ£¬Ê§°Ü¶ø²»ÊÇ´´½¨Ëü£¨Ö»Ó¦¸ÃʹÓà O_CREAT£©
O_TRUNC£ºÈç¹ûÎļþÒÑ´æÔÚ£¬´ÓÖгýÈ¥ËùÓÐÊý¾Ý£¨Óë´´½¨ÐÂÎļþÀàËÆ£©
open() µÄµÚÈý¸ö²ÎÊýÖ»ÔÚʹÓà O_CREAT ʱÐèÒª£»ËüÖ¸¶¨ÁËÒÔÊý×Ö±íʾµÄÎļþÐí¿ÉȨ£¨¸ñʽÓë chown ÃüÁîµÄÊýÖµÐí¿ÉȨ×Ô±äÁ¿µÄ¸ñʽÏàͬ¡£Îª open() Ö¸¶¨µÄÐí¿ÉȨÊÜÓû§µÄ umask Ó°Ï죬ºóÕßÔÊÐíÓû§Ö¸¶¨Ò»ÏµÁÐÐÂÎļþÓ¦¸Ã»ñµÃµÄȱʡÐí¿ÉȨ¡£´ó¶àÊý´´½¨ÎļþµÄ³ÌÐò¶¼Ê¹ÓõÚÈý¸ö×Ô±äÁ¿ 0666 µ÷Óà open()£¬¿ÉÒÔÈÃÓû§Í¨¹ý umask À´¿ØÖƳÌÐòµÄȱʡÐí¿ÉȨ¡££¨´ó¶àÊý shell µÄ umask ÃüÁ¿ÉÒÔ¸ü¸ÄËü¡££©
ÀýÈ磬Çåµ¥ 3 ÏÔʾÁËÈçºÎΪ½øÐжÁд´ò¿ªÎļþ¡¢Èç¹ûËü²»´æÔÚÔò´´½¨£¬ÒÔ¼°·ÏÆúÆäÖеÄÊý¾Ý£º
Çåµ¥ 3£º
int fd;
fd = open("myfile", O_RDWR | O_CREAT | O_TRUNC, 0666)
if (fd < 0) {
/* Some error occurred */
/* ... */
}
open() ·µ»ØÒýÓÃÎļþµÄÎļþÃèÊö·û¡£»ØÒäһϣ¬ÎļþÃèÊö·û×ÜÊÇ >= 0¡£Èç¹û open() ·µ»ØÁËÒ»¸ö¸ºÖµ£¬¾Í±íʾ·¢ÉúÁË´íÎó£¬È«¾Ö±äÁ¿´íÎóºÅ°üº¬ÁËÃèÊöÎÊÌâµÄ Unix ´íÎó´úÂë¡£open() ×ܾ¡Á¿·µ»Ø×îСÊý£¬Èç¹ûûÓÐʹÓÃÎļþÃèÊö·û 0£¬open() ½«×Ü·µ»Ø 0¡£
½ø³Ì´øÎļþ½áÊøʱ£¬ËüÓ¦¸Ãͨ¹ý close() ϵͳµ÷ÓùرÕËü£¬¸Ãϵͳµ÷ÓõĸñʽΪ£º
int close(int fd);
close µÄÎļþÃèÊö·ûÊÇ´«µÝ¸ø close() µÄΨһ×Ô±äÁ¿£¬Ôڳɹ¦Çé¿öÏ·µ»Ø 0¡£¾¡¹Ü close() ʧ°ÜµÄÇé¿ö±È½ÏÉÙ¼û£¬µ«Èç¹ûÎļþÃèÊö·ûÒýÓõÄÊÇÔ¶³Ì·þÎñÆ÷ÉϵÄÎļþ£¬ÏµÍ³ÎÞ·¨ÕýÈ·Çå¿ÕËüµÄ¸ßËÙ»º´æ£¬close() ¾Í¿ÉÄÜÕæµÄʧ°Ü¡£½ø³ÌÖÕֹʱ£¬ÄÚºË×Ô¶¯¹Ø±ÕËùÓл¹ÔÚ´ò¿ªµÄÎļþ¡£
×îºóµÄÒ»¸ö³£¼ûÎļþ²Ù×÷ÊÇÒƶ¯ÎļþÖ¸Õë¡£Õ⣨×ÔÈ»£©Ö»¶Ô´øÎļþÖ¸ÕëµÄÎļþÓÐÒâÒ壬Èç¹û³¢ÊÔÔÚ²»Ç¡µ±µÄÎļþÉϳ¢ÊԸòÙ×÷¾Í»á·µ»Ø´íÎó¡£lseek() ϵͳµ÷ÓÃÓÃÓÚÒÔÏÂÄ¿µÄ£º
off_t lseek(int fd, off_t pos, int whence);
off_t ÀàÐÍÊDZí´ï longint £¨long ¾ÍÊÇ lseek ÖÐ "l" µÄÀ´Àú£©µÄÒ»ÖÖ±ðÖ·½·¨¡£lseek() ·µ»ØÏà¶ÔÓÚÎļþ¿ªÊ¼´¦ÎļþÖ¸ÕëµÄ×îÖÕλÖã¬Èç¹ûÓдíÎó£¬Ôò·µ»Ø -1¡£Õâ¸öϵͳµ÷ÓÃÏ£Íû±»Òƶ¯µÄÎļþÖ¸ÕëËùÊôµÄÎļþÃèÊö·û×÷ΪµÚÒ»¸ö×Ô±äÁ¿£¬½«ËüÒƶ¯µ½ÎļþÖеÄλÖÃ×÷ΪµÚ¶þ¸ö×Ô±äÁ¿¡£×îºóÒ»¸ö×Ô±äÁ¿ÃèÊöÎļþÖ¸ÕëµÄÒƶ¯·½Ê½¡£
SEEK_SET ½«ËüÒƶ¯µ½´ÓÎļþ¿ªÊ¼ËãÆðµÄ pos ×Ö½Ú¡£
SEEK_END ½«ËüÒƶ¯µ½´ÓÎļþ½áβËãÆðµÄ pos ×Ö½Ú¡£
SEEK_CUR ´ÓËüµ±Ç°Î»ÖÿªÊ¼ÏòÎļþ½áβÒƶ¯ pos ×Ö½Ú¡£
open()¡¢close()¡¢write()¡¢read() ºÍ lseek() µÄ×éºÏΪ linux ÌṩÁË»ù±¾µÄÎļþ·ÃÎÊ API¡£ËäÈ»»¹ÓÐÐí¶àÆäËü²Ù×ÝÎļþµÄº¯Êý£¬µ«ÕâÀïÃèÊöµÄÊÇ×î³£Óõġ£
´ó¶àÊý³ÌÐòÔ±¶¼Ê¹ÓÃÊìϤµÄ ANSI C ¿âÎļþº¯Êý£¬ÀýÈç fopen() ºÍ fread()£¬¶ø²»ÊÇÔÚ´ËÃèÊöµÄµÍ¼¶ÏµÍ³µ÷Ó᣿ÉÒÔÔ¤¼ûµ½£¬fopen() ºÍ fread() ÊÇÔÚÓû§¼¶±ð¿âÖÐÕâЩϵͳµ÷ÓõĻù´¡ÉÏʵÏֵġ£ÈÔÈ»»á¾³£¿´µ½µÍ¼¶ÏµÍ³µ÷ÓõÄʹÓã¬ÌرðÊÇÔÚ¸ü¸´ÔӵijÌÐòÖС£Í¨¹ýÊìϤÕâЩÀý³ÌºÍ½Ó¿Ú£¬Äú¾Í¿ÉÒÔ³ÉΪһ¸öÕæÕýµÄ Unix ºÚ¿ÍÁË¡£
¹ØÓÚ×÷Õß
Erik Troan ÊÇ Red Hat Software µÄ¿ª·¢Õߣ¬linux Application Development Ò»ÊéµÄ×÷ÕßÖ®Ò»¡£¿ÉÒÔͨ¹ý ewt@redhat.com ÓëËûÁªÏµ¡£