一個簡單例子,可以參考一下:#include<stdio.h>#include<iostream>usingnamespacestd;//filename儲存的檔名voidsaveToFile(char*p,char*filename){FILE*fp;if((fp=fopen(filename,"wb"))==NULL){return;}else{fwrite(p,strlen(p),1,fp);}fclose(fp);}//從檔案讀取n個字元voidreadFromFile(char*filename,intn){inti=1;FILE*fp;if((fp=fopen(filename,"rb"))==NULL){return;}else{charp[100];fread(p,n,1,fp);p[n+1]="\0";printf("--%s\n",p);}}intmain(){char*p="Xxxx";saveToFile(p,"..\\1.bat");readFromFile("..\\1.bat",4);system("pause");return0;}
一個簡單例子,可以參考一下:#include<stdio.h>#include<iostream>usingnamespacestd;//filename儲存的檔名voidsaveToFile(char*p,char*filename){FILE*fp;if((fp=fopen(filename,"wb"))==NULL){return;}else{fwrite(p,strlen(p),1,fp);}fclose(fp);}//從檔案讀取n個字元voidreadFromFile(char*filename,intn){inti=1;FILE*fp;if((fp=fopen(filename,"rb"))==NULL){return;}else{charp[100];fread(p,n,1,fp);p[n+1]="\0";printf("--%s\n",p);}}intmain(){char*p="Xxxx";saveToFile(p,"..\\1.bat");readFromFile("..\\1.bat",4);system("pause");return0;}