[ÖØÒª]CÖÐfread()º¯ÊýµÄ·µ»ØÖµ
×î½üÓиöÏîÄ¿ÐèÒªÔÚUNIXÏÂÓÃC×ö£¬ºÜ¾ÃûÓùýCÁË£¬·¢ÏÖЩÎÊÌâÈÝÒ׸ã´í¡£
Õâ¸öÎÊÌâºÜÈÝÒ׸ã´í£¬²¢µ¼ÖºܶàÎÊÌ⣬ÐèҪǿµ÷µÄÊÇfreadº¯Êý·µ»ØµÄ²¢²»ÊÇ×Ö½ÚÊý¡£
realRead = fread(buf,item,count,fp) £¨Ã¿´Î¶Áitem´óСµÄÊý¾Ý¿é£¬·Öcount´ÎÀ´¶Á¡££©
¶øÊÇ·µ»ØµÄÊdzɹ¦ÓÐЧµÄ¶ÁÈ¡µÄitemÔªËصĸöÊý£¬¶ø³É¹¦¶ÁÈëµÄ×Ö½ÚÊýÔòÊÇrealRead * sizeof(item)
Ò»°ã˵À´realRead ÊÇСÓÚcountµÄ£¬ºÜÇɵÄÇé¿ö¾Í¸ÕºÃΪcount.³ý·ÇÎļþ´óС¸ÕºÃΪitem´óСµÄÕûÊý±¶¡£
·µ»ØµÄÊÇÕæʵ¶ÁÈëitemÔªËصĸöÊý£¬ËäÈ»¶ÁÁËcount´Î£¬µ«ÊÇÕæÕý¶Áµ½µÄÓÐЧ¸öÊýΪrealRead¸ö
Õæʵ¶ÁÈë×Ö½ÚÊý¾ÍΪrealRead*sizeof(item)
RETURN VALUE
fread and fwrite return the number of items successfully read or written (i.e., not the number of
characters). If an error occurs, or the end-of-file is reached, the return value is a short item
count (or zero).
fread does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3)
to determine which occurred.
fread£¨´ÓÎļþÁ÷¶ÁÈ¡Êý¾Ý£©
±íÍ·Îļþ #include<stdio.h>
¶¨Ò庯Êý size_t fread(void * ptr,size_t size,size_t count,FILE * stream);
º¯Êý˵Ã÷ fread()ÓÃÀ´´ÓÎļþÁ÷ÖжÁÈ¡Êý¾Ý¡£²ÎÊýstreamΪÒÑ´ò¿ªµÄÎļþÖ¸Õ룬²ÎÊýptr Ö¸ÏòÓû´æ·Å¶ÁÈ¡½øÀ´µÄÊý¾Ý¿Õ¼ä£¬¶ÁÈ¡µÄ×Ö½ÚÊýÒÔ²ÎÊýsize*countÀ´¾ö¶¨¡£
Fread()»á·µ»Øʵ¼Ê¶ÁÈ¡µ½µÄcountÊýÄ¿£¬Èç¹û´ËÖµ±È²ÎÊýcountÀ´µÃС£¬Ôò´ú±í¿ÉÄܶÁµ½ÁËÎļþβÁË»òÕßÓдíÎó·¢Éú(Ç°Õß¼¸ÂÊ´ó)£¬Õâʱ±ØÐëÓÃfeof()»òferror()À´¾ö¶¨·¢ÉúʲôÇé¿ö¡£
·µ»ØÖµ ·µ»Øʵ¼Ê¶ÁÈ¡µ½µÄcountÊýÄ¿¡£
fread·µ»ØµÄ²»ÊÇ×Ö½ÚÊý£¬
µ±ÇÒ½öµ±ÏÂÃæÕâôÓõÄʱºò£¬·µ»ØÖµ²ÅÊÇ×Ö½ÚÊý£¨µ±È»ÒòΪǡºÃÒ»¸öÊý¾Ý¿é´óСΪ1¸ö×Ö½Ú£¬Ï൱ÓÚrealRead*1£©
char buff[size];
FILE *fp;
...
realRead = fread(buff, 1, size, fp);
...
Èç¹ûÊÇ: fread(buff, size, 1, fp)
·
Ïà¹ØÎĵµ£º
CÊÇÒ»¸ö½á¹¹»¯ÓïÑÔ£¬ÈçÌ·ÀÏÒ¯×ÓËù˵£ºËüµÄÖصãÔÚÓÚËã·¨ºÍÊý¾Ý½á¹¹¡£C³ÌÐòµÄÉè¼ÆÊ×Òª¿¼ÂǵÄÊÇÈçºÎͨ¹ýÒ»¸ö¹ý³Ì£¬¶ÔÊäÈ루»ò»·¾³Ìõ¼þ£©½øÐÐÔËËã´¦ÀíµÃµ½Êä³ö£¨»òʵÏÖ¹ý³Ì£¨ÊÂÎñ£©¿ØÖÆ£©£¬¶ø¶ÔÓÚC++£¬Ê×Òª¿¼ÂǵÄÊÇÈçºÎ¹¹ÔìÒ»¸ö¶ÔÏóÄ£ÐÍ£¬ÈÃÕâ¸öÄ£ÐÍÄܹ»ÆõºÏÓëÖ®¶ÔÓ¦µÄÎÊÌâÓò£¬ÕâÑù¾Í¿ÉÒÔͨ¹ý»ñÈ¡¶ÔÏóµÄ״̬ÐÅÏ¢µÃµ½Êä³ö»òʵÏÖ¹ ......
º¯Êý - ÉùÃ÷¡¢¶¨Òå¡¢µ÷ÓÃ
¡¡¡¡1. Èç¹ûº¯ÊýûÓÐÉùÃ÷, Ó¦¸ÃÔÚµ÷ÓÃÇ°¶¨Òå:
#include¡¡<stdio.h>
/*¡¡¶¨ÒåÇó´óÖµº¯Êý¡¡*/
int¡¡MAX(int¡¡x,¡¡int¡¡y)¡¡{
¡¡¡¡if¡¡(x¡¡>¡¡y)
¡¡¡¡¡¡¡¡return¡¡x;
¡¡¡¡else
¡¡¡¡¡¡¡¡return¡¡y;
}¡¡
/*¡¡¶¨ÒåÇóСֵº¯Êý¡¡*/
int¡¡MIN(int¡¡x,¡¡int¡¡y)¡¡{
¡¡¡¡return¡¡x¡¡&l ......
±àÕß°´:·Ç³£»ù±¾¹ØÓÚCÓïÑÔµÄÎÊÌâ,Ò»¸öÐÅÏ¢Àà(¼ÆËã»ú,×ÊѶ¹¤³Ì,µç×Ó¹¤³Ì, ͨÐÅ
¹¤³Ì)רҵµÄ±¾¿Æ±ÏÒµÉúÓ¦¸Ã´ïµ½µÄˮƽ¡£ÌâÄ¿²»ÄÑ,È«²¿¶¼ÄÜ¿ìËٵشðÍ꣬µ±È»Ò²ÐèÒª
Ò»¶¨µÄ֪ʶ´¢±¸¡£
¶ÔÓÚ´ó¶àÊýÈË£¬ÎÒÃÇÔ¤ÆÚÄã¿ÉÄÜ´ð´í 1) 4) 15)Ì⣬ËùÒÔ´ð´í3µÀÒÔÄڵģ¬ÎÒÃÇÈÏΪ
ÄãºÜ°ô
´ð´í5µÀÌâÒÔ ......
ÕæÕý·¢ÏÖÈËÉúÐèҪ֪ʶµÄʱºò£¬ÎÒµÄÇà´ºÒѾÀË·ÑÁËÒ»°ë¡£
ÓÚÊǹÄÆðÓÂÆøÀ´µ½±±´óÇàÄñѧϰIT£¬¼øÓڶ෽¿¼ÂÇ£¬»¹ÊÇÑ¡ÔñÁËÈí¼þ²âÊÔ¡£
Èç½ñ¿ÎÒµµÄÒ»´ó°ëÒ²ÒѽáÊø£¬´Ó¸Õ¿ªÊ¼µÄÒ»ÎÞËùÖª£¬½¥½¥³É³¤ÎªÒ»¸ö²ËÄñ……
Ôڿγ̵Äѧϰ¹ý³ÌÖУ¬²Å½¥½¥·¢ÏÖ×Ô¼º¶ÔÓÚÕâ¸öÐÐÒµµÄÐËȤ£¬ÓÈÆäÊÇÈí¼þ¿ª·¢ÕâÒ»¿é¡£
ÉõÖÁ¶Ô×Ô¼ºÑ¡Ôñ×ö²â ......