<The C programming language> 语言测试 2

来源:互联网 发布:中国经纪人网络登录 编辑:程序博客网 时间:2024/05/21 08:51

测试以下内容:

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");}

0 0
原创粉丝点击