测试以下内容:
1、文件读写操作:“在读和写的交叉过程中,必须调用fflush函数或文件定位函数”
2、remove()
3、rename()
4、tmpnam()
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void){ char *chp = 0; PRintf("TMP_MAX = %d/n",TMP_MAX); for(int i = 0;i < 10;i++){ chp = tmpnam(NULL); printf("%s/n",chp); } char c, *p = chp; int i = 0; while ((c = *p++) && c != EOF) if (c == '//') i = p - chp; char *tmp = chp + i; printf("/nThe last temp file name is %s./n",tmp);// for(i = 0;i < 100;i++)// rand(); int r; r = rand(); FILE *fp; fp = fopen(tmp,"a+"); fprintf(fp,"%i",r); fseek(fp,0,SEEK_SET); int rr; fscanf(fp,"%d",&rr); printf("The random number is %d./n",rr); if (fclose(fp)) printf("Can't close the file./n"); if (!rename(tmp,"new.txt")) printf("file name CHANGED HERE!/n"); remove("new.txt");}
新闻热点
疑难解答