- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
tmpfile() function in C
The function tmpfile() creates a temporary file in binary update mode in C. It initializes in header file of a C program. It always returns a null pointer if the temporary file cannot be created. The temporary file deleted automatically just after the termination of program.
Syntax
FILE *tmpfile(void)
Return value
If file creation is successful, the function returns a stream pointer to the temporary file created. If the file cannot be created, NULL pointer is returned.
Algorithm
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.
Example
#include <stdio.h> int main() { char c[] = "Tutorials Point"; 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 }
Output
Temporary file created successfully Tutorials Point
Advertisements
To Continue Learning Please Login
Login with Google