ÎļþµÄ´ò¿ªÓë¹Ø±Õ£¬¶ÁÓëд²Ù×÷µÄÏà¹Øº¯Êý£¬ÔÚ¶ÔÎļþ²Ù×÷֮ǰ£¬±ØÐ붨ÒåÒ»¸öÎļþÖ¸Õ룬ÕâÑùµÄ»°£¬²ÅÄܶԸÃÎļþ½øÐвÙ×÷¡£cÓïÑÔÖеÄÎļþ²Ù×÷Ïà¹Øº¯ÊýµÄÍ·Îļþ¶¼ÊÇ#include<stdio.h>
FILE * fp£» //¿ÉÒÔÊÇÈκÎÆäËûÖ¸ÕëÃû
1£©open()º¯Êý
º¯Êý¹¦ÄÜ£ºÎļþ´ò¿ªº¯Êý
º¯ÊýÔÐÍ£ºfp=fopen(ÎļþÃû£¬Ê¹ÓÃÎļþ·½Ê½);
FILE *fopen(const char *path,const char *mode); //ÎļþÃû ģʽ
º¯Êý˵Ã÷£ºÈç¹ûÊǵ±Ç°¹¤×÷Ŀ¼ÏµÄÎļþ£¬¿ÉÒÔ²»ÓüÓ·¾¶Ãû£¬Èç¹ûÊÇÆäËûĿ¼ÏµÄÔòÐèÒª¼Ó·¾¶
º¯Êý²ÎÊý£ºÒÔmodeµÄ·½Ê½´ò¿ª»ò´´½¨Îļþ
º¯Êý·µ»ØÖµ£ºÈç¹û³É¹¦£¬½«·µ»ØÒ»¸öÎļþÖ¸Õ룬ʧ°ÜÔò·µ»ØNULL£¨Æäʵ¾ÍÊÇ0£©
º¯Êý×¢Ò⣺ ÎļþÃûºÍ´ò¿ªµÄ·½Ê½ÔÚдµÄʱºò£¬±ØÐëÓÃË«ÒýºÅÀ¨ÆðÀ´
ÔÚ Linux ϵͳÖÐ,mode ÀïÃæµÄ¡¯b¡¯(¶þ½øÖÆ)¿ÉÒÔÈ¥µô£¬µ«ÊÇΪÁ˱£³ÖÓëÆäËûϵͳµÄ¼æÈÝÐÔ£¬½¨Ò鲻Ҫȥµô¡£ abºÍ a+b Ϊ׷¼Óģʽ£¬ÔÚ´ËÁ½ÖÖģʽÏ£¬ÎÞÂÛÎļþ¶Áдµã¶¨Î»µ½ºÎ´¦£¬ÔÚдÊý¾Ýʱ¶¼½«ÊÇ
ÔÚÎļþĩβÌí¼Ó£¬ËùÒԱȽÏÊʺÏÓÚ¶à½ø³Ìдͬһ¸öÎļþµÄÇé¿öϱ£
Ö¤Êý¾ÝµÄÍêÕûÐÔ¡£
ʹÓÃÎļþµÄ·½Ê½ÈçÏ£º
¡°r¡±(Ö»¶Á)£ºÎªÊäÈë´ò¿ªÒ»¸öÎı¾Îļþ ¶ÔÎļþ½øÐжÁ²Ù×÷
¡°w¡±(ֻд)£ºÎªÊä³ö´ò¿ªÒ»¸öÎı¾Îļþ ¶ÔÎļþ½øÐÐд²Ù×÷
¡°a¡±(×·¼Ó): ÏòÎı¾ÎļþβÌí¼ÓÊý¾Ý
¡°rb¡±(Ö»¶Á)£º ΪÊäÈë´ò¿ªÓÐÒ»¸ö¶þ½øÖÆÎļþ
¡°wb¡±(ֻд)£ºÎªÊä³ö´ò¿ªÒ»¸ö¶þ½øÖÆÎļþ¶ÔÎļþ½øÐÐд²Ù×÷
¡°ab¡±(×·¼Ó): Ïò¶þ½øÖÆÎļþβÌí¼ÓÊý¾Ý
¡°r+¡±(¶Áд)£º Ϊ¶Áд´ò¿ªÒ»¸öÎı¾Îļþ
¡°w+¡±(¶Áд)£ºÎª¶Áд½¨Á¢Ò»¸öеÄÎı¾Îļþ
¡°a+¡±(¶Áд): ÏòÎı¾ÎļþβÌí¼ÓÊý¾Ý
¡°rb+¡±(¶Áд)£º Ϊ¶Áд´ò¿ªÒ»¸ö¶þ½øÖÆÎļþ
¡°wb+¡±(¶Áд)£ºÎª¶Áд½¨Á¢Ò»¸öеĶþ½øÖÆÎļþ
¡°ab+¡±(¶Áд): Ϊ¶Áд´ò¿ªÒ»¸ö½øÖÆÎļþ
ģʽ ¶Áд λÖà ½Ø¶ÏÔÄÚÈÝ ´´½¨
rb Y N ÎļþÍ· N N
r+b Y Y ÎļþÍ· NN
wb N Y ÎļþÍ· Y Y
w+b Y Y ÎļþÍ· YY
ab N Y Îļþβ N Y
a+b Y Y Îļþβ N Y
2£©fclose()º¯Êý
º¯Êý¹¦ÄÜ£ºÎļþµÄ¹Ø±Õ
º¯ÊýÔÐÍ£ºfclose(ÎļþÖ¸Õë)
int fclose(FILE *stream)
º¯Êý·µ»ØÖµ£º³É¹¦¹Ø±ÕÎļþ·µ»ØֵΪ0£¬·ñÔò·µ»ØEOF(-1)£»
º¯Êý˵Ã÷£ºÊ¹ÓÃÍêÒ»¸öÎļþ£¬ÒÔ·ÀÖ¹Ôٴα»ÎóÓ㬹رգ¬¾ÍÊÇÖ¸ÎļþÖ¸Õë±äÁ¿²»ÔÙÖ¸Ïò¸ÃÎļþ£¬·ÀÖ¹Êý¾Ý¶ªÊ§
»ùÓÚÎļþÖ¸ÕëµÄ¶Áдº¯ÊýºÜ¶à£¬¿É·ÖΪÊý¾Ý¿é¶Áд¡¢¸ñʽ»¯¶Áд¡¢µ¥¸ö×Ö·û¶Áд¡¢×Ö·û´®¶Áд
3£©fread()º¯Êý
º¯Êý¹¦ÄÜ£ºÊý¾Ý¿é¶Á£¬¶ÁÈ¡¶à¸öÊý¾Ý
º¯ÊýÔÐÍ£ºsize_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream);
fread(buffer,size,count,fp);
º¯Êý˵Ã÷£ºfread ´ÓÎļþÁ÷ stream ÖжÁÈ¡ nmemb ¸öÔªËØ£¬Ð´µ½ ptr Ö¸ÏòµÄÄÚ´æÖУ¬Ã¿¸öÔªËصĴóСΪ size ¸ö×Ö½Ú¡£
º¯Êý×¢Ò⣺ËùÓеÄÎļþ¶Áдº¯Êý¶¼´ÓÎļþµÄµ±Ç°¶Áдµã¿ªÊ¼¶Áд£¬¶ÁдÍêÒԺ󣬵±Ç°¶Áдµã×Ô¶¯ÍùºóÒƶ¯ size*nmemb¸ö×Ö½Ú
º¯Êý·µ»ØÖµ£º·µ»ØΪ1±íʾ¶ÁÈ¡ÁËsize¸ö×Ö½Ú£¬·µ»Ø0±íʾ¶ÁÈ¡²»¹»size×Ö½Ú
4£©fwrite()º¯Êý
º¯Êý¹¦ÄÜ£ºÊý¾Ý¿éд£¬Ð´Èë¶à¸öÊý¾Ý
º¯ÊýÔÐÍ£ºsize_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
fwrite(buffer,size,count,fp)
º¯Êý˵Ã÷£º fwrite ´Ó ptr Ö¸ÏòµÄÄÚ´æÖжÁÈ¡ nmemb ¸öÔªËØ£¬Ð´µ½ÎļþÁ÷ stream ÖУ¬Ã¿¸öÔªËØ size ¸ö×Ö½Ú
º¯Êý·µ»ØÖµ£º·µ»ØËùдµÄÊý¾Ý¿éµÄÊýÄ¿£¬nmemb£¨count£©£¨Êµ¼ÊÖµ£©
µ÷Óò»³É¹¦·µ»ØÖµÊÇ0
×¢Ò⣺ÒÔÉÏ£¬buffer²ÎÊý£¬¶ÔfreadÀ´Ëµ£¬ËüÊǶÁÈëÊý¾ÝµÄ´æ·ÅµØÖ·£¬¶ÔfwriteÀ´Ëµ£¬ÊÇÊä³öÊý¾ÝµÄµØÖ·
ʾÀý£º
#include <stdio.h>
int main()
{
char buf[50] = {'h','e','l','l','o'};
FILE *p;//¶¨ÒåÒ»¸ö FILE ½á¹¹ÌåÀàÐ͵ÄÖ¸Õë p
p = fopen("a.txt", "r+b");//p Õâ¸öÖ¸Õë´Ëʱ¾ÍºÍÎļþ a.txt ¹ØÁªÆðÀ´ÁË¡£
fwrite(buf, 1, 5, p);//°Ñ buf ÀïÃæµÄÄÚÈÝдµ½ p Ö¸ÏòµÄÎļþÖС£
char buf1[50] = {0};
fread(buf1, 1, 5, p);
printf("buf1:%s\n", buf1);
fclose(p);//¹Ø±Õ p ´ú±íµÄÎļþ a.txt
return 0;
}
×¢Ò⣺ÔÚWindowsϲÙ×÷µÄʱºò£¬Îļþ·¾¶ÊÇË«·´Ð±¸Ü¡±\\¡±,
ÒòΪµ¥¸ö·´Ð±ÏßΪתÒå·û£¬ÕâÑù´ò²»¿ªÎļþ
#include <stdio.h>
int main()
{
char buf[50] = {'h','e','l','l','o'};
char buf1[50] = {0};
FILE *p;//¶¨ÒåÒ»¸ö FILE ½á¹¹ÌåÀàÐ͵ÄÖ¸Õë p
p = fopen("E:\\ÐÅÓ¯´ïSchool\\CÓïÑÔ\\2017.02.13È«ÈÕÖÆCÓïÑÔ\\12µÚ12ÌìÁ´±í\\5.ʾÀý´úÂë\\a.txt", "r+b");//p Õâ¸öÖ¸Õë´Ëʱ¾ÍºÍÎļþ a.txt ¹ØÁªÆðÀ´ÁË¡£
fwrite(buf, 1, 5, p);//°Ñ buf ÀïÃæµÄÄÚÈÝдµ½ p Ö¸ÏòµÄÎļþÖС£
fseek(p, 0, SEEK_SET);
fread(buf1, 1, 5, p);
printf("buf1:%s\n", buf1);
fclose(p);//¹Ø±Õ p ´ú±íµÄÎļþ a.txt
return 0;
}
×¢Ò⣺ÉÏÃæµÄÀý×ӾͻᷢÏÖÆäʵ²¢Ã»ÓжÁµ½ÈκÎÄÚÈÝbuf1δÏÔʾ³öÄÚÈÝ
ÊÇÒòΪ´ËʱÎļþÖ¸ÕëÒѾÅܵ½ÎļþĩβÁË£¬¿ÉÒÔͨ¹ýÔÚ¶¨ÒåÒ»¸öÎļþÖ¸ÕëµÄ·½Ê½£¬Ê¹ÆäÖ´ÐÐÎļþ¿ªÍ·£¬¾Í¿ÉÒÔ·ÃÎʵ½ÄÚÈÝ
FILE *fp;//¶¨ÒåÒ»¸ö FILE ½á¹¹ÌåÀàÐ͵ÄÖ¸Õë fp
p = fopen("a.txt", "r+b");//fp Õâ¸öÖ¸Õë´Ëʱ¾ÍºÍÎļþ a.txt ¹ØÁªÆðÀ´ÁË¡£
ÁíÍâÒ»ÖÖ·½Ê½¾ÍÊÇÎļþ¶¨Î»fseekº¯Êý