Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 20th, 2005, 3:46 AM   #1
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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]
__________________

tempest is offline   Reply With Quote
Old Oct 20th, 2005, 10:58 AM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
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.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC