1 #define _CRT_SECURE_NO_WARNINGS
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 void main()
6 {
7 // tmpfile产生临时文件,关闭文件或者程序关闭,就会自动删除
8 FILE *ptemp = tmpfile();// 创建临时文件,返回文件指针
9 if (ptemp == NULL)//文件指针为空,就意味着创建失败
10 {
11 printf("临时文件创建失败");
12 return;
13 }
14
15 fputs("学C真TM苦,一本小破书,一看一下午", ptemp);//写入
16 rewind(ptemp); // 回到文件开头,进行文件的读取
17
18 char str[512];
19 fgets(str, 512, ptemp); // 获取字符串
20 puts(str); //屏幕输出
21
22 fclose(ptemp);// 关闭文件
23
24 system("pause");
25 }
1 /* tmpnam 创建临时文件名 */
2
3 #define _CRT_SECURE_NO_WARNINGS
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 void main()
8 {
9 FILE *ptemp; // 创建一个文件指针
10 char path[100]; // 路径,保存临时文件
11 tmpnam(path); // 生成一个临时文件名,保存到path
12 ptemp = fopen(path, "w+"); // 按照可读可写的方式打开路径
13 printf("路径是%s", path);
14 if (ptemp == NULL)
15 {
16 printf("文件打开失败");
17 }
18
19 // 写入文本
20 fputs( "学编程,学语言,学语法,学算法,没完没了\n", ptemp);
21 rewind(ptemp);// 文件指针移动到开头
22 char str[512];
23 fgets(str, 512, ptemp);// 从文件读取内容
24
25 printf("%s", str);// 输出字符串
26 fclose(ptemp);
27
28
29 system("pasue");
30 }