C中的tmpfile()函数
该函数tmpfile()在C中以二进制更新模式创建一个临时文件。它在C程序的头文件中初始化。如果无法创建临时文件,它将始终返回空指针。程序终止后,临时文件会自动删除。
语法
FILE *tmpfile(void)
返回值
如果文件创建成功,该函数将返回指向创建的临时文件的流指针。如果无法创建文件,则返回NULL指针。
算法
Begin.
Declare an array variable c[] to the character datatype and take a character data string.
Initialize a integer variable i ← 0.
Declare a newfile pointer to the FILE datatype.
Call tmpfile() function to make newfile filepointer as temporary file.
Call open() function to open “nfile.txt” to perform write operation using newfile file pointer.
if (newfile == NULL) then
print “Error in creating temporary file” .
return 0.
Print “Temporary file created successfully”.
while (c[i] != '\0') do
put all the data of c[] into the filepointer newfile.
i++.
Call fclose() function to close the file pointer.
Call open() function to open “nfile.txt” to perform read operation using newfile file pointer.
Call rewind() function to set the pointer at the beginning of the stream of the file pointer.
while (!feof(newfile)) do
call putchar() function to print all the data of file pointer newfile.
Call fclose() function to close the file pointer.
End.示例
#include <stdio.h>
int main() {
char c[] = "nhooo.com";
int i = 0;
FILE* newfile = tmpfile(); //make the file pointer as temporary file.
newfile = fopen("nfile.txt", "w");
if (newfile == NULL) {
puts("Error in creating temporary file");
return 0;
}
puts("Temporary file created successfully");
while (c[i] != '\0') {
fputc(c[i], newfile);
i++;
}
fclose(newfile);
newfile = fopen("nfile.txt", "r");
rewind(newfile); //set the pointer at the beginning of the stream of the file pointer.
while (!feof(newfile))
putchar(fgetc(newfile));
fclose(newfile); //closing the file pointer
}输出结果
Temporary file created successfully nhooo.com
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短