1.1 ±ê×¼IOµÄÎļþ²Ù×÷º¯Êý
1.1.1 fopen
Í·Îļþ£º#include <stdio.h>
º¯ÊýÔÐÍ£ºFILE *fopen(const char *path, constchar *mode);
º¯Êý¹¦ÄÜ£º´ò¿ªÒ»¸öÎļþpath£¬»ñÈ¡µ½´ËÎļþµÄÎļþÁ÷Ö¸Õë
ÎļþÁ÷Ö¸Õ룺FILE*
typedef struct file_buf{
int fd;
char buf[size];
} FILE ;
ÐβÎÁÐ±í£º
path£ºÒª´ò¿ªÎļþÁ÷Ö¸ÕëµÄÎļþ
mode£º²Ù×÷·½Ê½
r £ºÖ»¶Á£¨²»ÄÜ´´½¨Îļþ£©
r+ £º¶Áд£¨¸²¸Çд£¬²»ÄÜ´´½¨Îļþ£©
w £ºÖ»Ð´£¨Çå¿Õд£¬¿ÉÒÔ´´½¨Îļþ£©
w+ £º¶Áд£¨Çå¿Õд£¬¿ÉÒÔ´´½¨Îļþ£©
a £ºÖ»Ð´£¨×·¼Óд£¬¿ÉÒÔ´´½¨Îļþ£©
a+ £º¶Áд£¨×·¼Óд£¬¿ÉÒÔ´´½¨Îļþ£©
·µ»ØÖµ£º
³É¹¦£ºÎļþÁ÷Ö¸Õë
ʧ°Ü£ºNULL
д´½¨µÄÎļþ£¬È¨ÏÞĬÈÏΪ0666
1.1.2 fclose
º¯ÊýÔÐÍ£ºint fclose(FILE*fp);
º¯Êý¹¦ÄÜ£º¹Ø±ÕfpÎļþÁ÷Ö¸Õë
ÐβÎÁÐ±í£º
fp£ºÒª¹Ø±ÕµÄÎļþÁ÷Ö¸Õë
·µ»ØÖµ£º
³É¹¦£º0
ʧ°Ü£ºEOF
1.1.3 fprintf
º¯ÊýÔÐÍ£ºint fprintf(FILE*stream, const char *format, ...);
º¯Êý¹¦ÄÜ£ºÍùstreamÎļþÁ÷Ö¸Õ룬°´ÕÕconstchar *format¸ñʽдÈëÊý¾Ý
ÐβÎÁÐ±í£º
stream£ºÒªÐ´ÈëµÄÎļþÁ÷Ö¸Õë
const char *format£º¸ñʽ¿ØÖÆ£¬ÓëprintfÓ÷¨Ò»ÖÂ
·µ»ØÖµ£º
³É¹¦£º×Ö½ÚÊý
ʧ°Ü£º¸ºÊý
ºÍprintfÓ÷¨»ù±¾Ò»Ö£¬printfÊÇÍùÖÕ¶ËÊä³öÊý¾Ý£¬¶øfprintfÊÇÍùstreamдÈëÊý¾Ý£¬Èç¹ûstreamÎļþÁ÷Ö¸ÕëÖ¸Ïòstdout£¬ÄÇô¸úprintfÒ»ÑùµÄЧ¹û
1.1.4 fscanf
º¯ÊýÔÐÍ£ºint fscanf(FILE*stream, const char *format, ...);
º¯Êý¹¦ÄÜ£º´ÓstreamÎļþÁ÷Ö¸ÕëÖ¸ÏòµÄÎļþÖУ¬»ñÈ¡const char *format¸ñʽµÄÊý¾Ý
ÐβÎÁÐ±í£º
stream£ºÐèÒª»ñÈ¡Êý¾ÝµÄÎļþÁ÷Ö¸Õë
const char *format£ºÒª»ñÈ¡µÄÊý¾Ý¸ñʽ
·µ»ØÖµ£º
³É¹¦£º×Ö½ÚÊý
ʧ°Ü£ºEOF
ºÍscanfÓ÷¨»ù±¾Ò»Ö£¬scanfÊÇ´ÓÖն˻ñÈ¡Êý¾Ý£¬¶øfscanfÊÇ´Óstream»ñÈ¡Êý¾Ý£¬Èç¹ûstreamÖ¸Ïòstdin£¬ÄÇô¸úscanfÒ»ÑùµÄЧ¹û
1.1.5 fgets
º¯ÊýÔÐÍ£ºchar *fgets(char*s, int size, FILE *stream);
º¯Êý¹¦ÄÜ£º´ÓstreamÖ¸ÏòµÄÎļþÖУ¬»ñÈ¡size¸ö×Ö·û£¬´æ´¢µ½s»º´æÇøÖÐ
ÐβÎÁÐ±í£º
s £º´æ´¢»ñÈ¡µ½µÄÊý¾ÝµÄ»º´æÇø
size £º»ñÈ¡¸öÊý
stream £ºÎļþÁ÷Ö¸Õë
·µ»ØÖµ£º
³É¹¦£º»º´æÇøµÄÖ¸Õë
ʧ°Ü£ºNULL
ºÍgetsÓ÷¨»ù±¾Ò»Ö£¬getsÊÇ´ÓÖն˻ñÈ¡Êý¾Ý£¬¶øfgetsÊÇ´Óstream»ñÈ¡Êý¾Ý£¬Èç¹ûstreamÖ¸Ïòstdin£¬ÄÇô¸úgetsÒ»ÑùµÄЧ¹û
1.1.6 fputs
º¯ÊýÔÐÍ£ºint fputs(constchar *s, FILE *stream);
º¯Êý¹¦ÄÜ£ºÍùstreamÎļþÁ÷Ö¸ÕëÖ¸ÏòµÄÎļþÊä³ös»º´æÇøÖеÄÊý¾Ý
ÐβÎÁÐ±í£º
s£ºÒªÊä³öµÄÊý¾Ý»º´æÇø
stream£ºÒªÊä³öµÄ¶ÔÏóÎļþÁ÷Ö¸Õë
·µ»ØÖµ£º
³É¹¦£º·Ç¸ºÊý
ʧ°Ü£ºEOF
ºÍputsÓ÷¨»ù±¾Ò»Ö£¬putsÊÇ´ÓÖն˻ñÈ¡Êý¾Ý£¬¶øfputsÊÇ´Óstream»ñÈ¡Êý¾Ý£¬Èç¹ûstreamÖ¸Ïòstdout£¬ÄÇô¸úputsÒ»ÑùµÄЧ¹û£¬Î¨Ò»µÄÇø±ðÔÚÓÚputs»á×Ô¶¯¼ÓÉÏ»»Ðзû£¬¶øfputs²»»á×Ô¶¯¼ÓÉÏ¡£
1.1.7 fread
º¯ÊýÔÐÍ£ºsize_tfread(void *ptr, size_t size, size_t nmemb, FILE *stream);
º¯Êý¹¦ÄÜ£º´ÓstreamÎļþÁ÷Ö¸ÕëÖ¸ÏòµÄÎļþÖУ¬¶ÁÈ¡size*nmemb¸ö×Ö½ÚÊý¾Ý£¬´æ´¢µ½ptrÖ¸ÏòµÄ»º´æÇøÖÐ
ÐβÎÁÐ±í£º
ptr£º´æ´¢Êý¾ÝµÄ»º´æÇø
size£ºÒ»¿éÊý¾ÝµÄ×Ö½Ú´óС
nmemb£º¿éÊý£¬ÓжàÉÙ¿ésize´óСµÄÊý¾Ý
stream£ºÐèÒª¶ÁÈ¡Êý¾ÝµÄÎļþÁ÷Ö¸Õ루Êý¾ÝÀ´Ô´£©
·µ»ØÖµ£º
³É¹¦£º¶ÁÈ¡µ½µÄ¿éÊý£¬²»¹»Ò»¿éµÄ²»ËãÒ»¿é
ʧ°Ü/Îļþĩβ£º0
ÀýÈ磺fread(buf,100, 5, fp);
freadº¯Êý£¬¿ÉÒÔ¶ÁÈ¡µ½»Ø³µ£¬²»»áÒòΪ»Ø³µ¶øÍ£Ö¹¶ÁÈ¡£¬Ò²²»»áÒòΪ¿Õ¸ñ¶øÍ£Ö¹¶ÁÈ¡
1.1.8 fwrite
º¯ÊýÔÐÍ£ºsize_tfwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
º¯Êý¹¦ÄÜ£ºÍùstreamÎļþÁ÷Ö¸ÕëÖ¸ÏòµÄÎļþÖУ¬Ð´Èësize*nmemb¸ö×Ö½ÚµÄÊý¾Ý£¬Êý¾ÝÀ´Ô´ÊÇptrÖ¸ÕëÖ¸ÏòµÄ»º´æÇø
ÐβÎÁÐ±í£º
ptr£ºÖ¸ÏòҪдÈëµÄÊý¾ÝÀ´Ô´µÄÖ¸Õë
size£ºÒ»¿éÊý¾ÝµÄ×Ö½Ú´óС
nmemb£º¿éÊý£¬ÓжàÉÙ¿ésize´óСµÄÊý¾Ý
stream£ºÖ¸Ïò¶ÔÏóÎļþµÄÎļþÁ÷Ö¸Õë
·µ»ØÖµ£º
³É¹¦£ºÐ´ÈëµÄ¿éÊý
ʧ°Ü£º0
ÀýÈ磺fwrite(buf,100, 5, fp);
fwiteº¯Êý´ÓptrÖ¸ÏòµÄ»º´æÇøÖлñÈ¡Êý¾ÝдÈëµ½streamÖУ¬Óöµ½»Ø³µ»òÕß¿Õ¸ñ£¬²»»áÍ£Ö¹´ÓptrÖлñÈ¡Êý¾Ý£¬ÐβÎÒªÇó»ñÈ¡¶àÉÙ¸ö×Ö½Ú£¬¾Í»ñÈ¡¶àÉÙ¸ö×Ö½Ú£¬Èç¹ûptrÖ¸ÏòµÄ»º´æÇøÖÐÊý¾Ý²»¹»£¬fwite»á´ÓptrºóÔ½½çÍùÏ»ñÈ¡Êý¾Ý£¬Ð´Èëµ½streamÖÐ
1.1.9 fseek
º¯ÊýÔÐÍ£ºint fseek(FILE*stream, long offset, int whence);
º¯Êý¹¦ÄÜ£º°ÑÎļþÆ«ÒÆÖ¸Õ룬´Ówhence»ù×¼µã£¬Æ«ÒÆoffset¸ö×Ö½Úµ¥Î»
ÐβÎÁÐ±í£º
stream£ºÎļþÁ÷Ö¸Õë
offset£ºÆ«ÒƵĵ¥Î»Á¿
whence£ºÆ«ÒƵĻù×¼µã
SEEK_SET£ºÎļþ¿ªÍ·
SEEK_CUR£ºÆ«ÒÆÖ¸Õ뵱ǰλÖÃ
SEEK_END£ºÎļþĩβ
·µ»ØÖµ£º
³É¹¦£º0
ʧ°Ü£º-1
fseekº¯ÊýÓëlseekº¯ÊýÇø±ðÔÚÓÚ·µ»ØÖµ
1.1.10 ftell
º¯ÊýÔÐÍ£ºlong ftell(FILE*stream);
º¯Êý¹¦ÄÜ£º·µ»ØÎļþÆ«ÒÆÖ¸ÕëÓëÎļþ¿ªÍ·µÄ¾àÀë
ÐβÎÁÐ±í£º
stream£ºÎļþÁ÷Ö¸Õë
·µ»ØÖµ£º
³É¹¦£ºÎļþÆ«ÒÆÖ¸Õ뵱ǰƫÒÆÁ¿£¬Ò²¾ÍÊÇÎļþÆ«ÒÆÖ¸ÕëÓëÎļþ¿ªÍ·µÄ¾àÀë
ʧ°Ü£º-1
1.1.11 rewind
º¯ÊýÔÐÍ£ºvoid rewind(FILE*stream);
º¯Êý¹¦ÄÜ£º°ÑÎļþÆ«ÒÆÖ¸Õ룬ָÏòÎļþ¿ªÍ·
ÐβÎÁÐ±í£º
stream£ºÎļþÁ÷Ö¸Õë
·µ»ØÖµ£ºÎÞ
rewind(fp)== fseek(fp,0,SEEK_SET);