From the course: C Programming: Exploring Advanced Concepts and Practical Applications

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Making a temporary file

Making a temporary file

- [Presenter] This code uses the mkstemp function to generate a temporary file name. The template is defined here. It can be any text you want, but it must end with six big Xs. These are replaced by randomly generated characters with a prefix unchanged. This function uses this template as its argument, and the value is returned as a file descriptor, not a file handle. It's closed here. The temporary file is created and named in the output. It also appears here in the file explorer. This file shares its name with no other file in the directory, so you can use it temporarily. Say you need to create 10 temporary files based on the same template. This code uses a loop to call the mkstemp function 10 times, each time putting the name and then closing the file descriptor. The program catches the error. The error is an invalid argument. What happens is that the template is replaced by the file name, which is output here, because the template is now invalid, it has no big X characters. The…

Contents