![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
Code snippet: Temporary files in C...
[php]
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { // Template for unique file name... // 'X' is a random character when mkstemp(char *) is // called but it has to contain 6 consequtive 'X's at // the end of the template otheriwse - seg fault char tmpName[] = "/tmp/fileXXXXXX"; // declaration: int mkstemp(char *template); // mkstemp returns the file descriptor opened // by fdopen(int, char *) which returns a FILE *. FILE *tmp = fdopen(mkstemp(tmpName), "w+"); // Put "Hello, world!\n", in temporary file... fputs("Hello, world!\n", tmp); // Close temporary file... fclose(tmp); // Show the contents of the temporary file... execlp("cat", "cat", tmpName, NULL); // All is good, bye now... return EXIT_SUCCESS; } [/php]
__________________
|
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Temporary files in C/POSIX, you mean. C only defines tmpfile and tmpnam, both of which are sufficient for what you used POSIX-only functions to achieve without sacrificing portability.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|