You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 12:21.

CS000889 - Creating temporary files in Open C

From Forum Nokia Wiki



ID CS000889 Creation date April 7, 2008
Platform S60 3rd Edition Tested on devices Nokia N93
Category Open C Subcategory Files/Data


Keywords (APIs, classes, methods, functions): tmpnam(), tmpfile()

Overview

Programs often need to create temporary files just for the life time of the program. In Open C tmpnam() and tempfile() functions exist to assist in this task.

  • tmpnam() generates file names that can be used for a temporary file.
  • tmpfile() creates a temporary file and opens a corresponding stream to the created file.

The tmpnam() and tmpfile() functions return a pointer to a file name on success or NULL pointer in case of an error.

Note: In order to use this code, you need to install the Open C plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  libc.lib

Source file

#include <stdio.h> //fprintf, tmpnam, tmpfile, FILE
#include <sys/stat.h> //S_IWUSR
 
int main (void)
{
char *tmp_pathname = 0;
char buffer[100];
 
FILE *tmp_fileptr = 0;
FILE *tmp_filestream = 0;
 
/* create the tmp directory */
mkdir("c:\\tmp", S_IWUSR);
 
/* - tmpnam - */
if (!(tmp_pathname = tmpnam(NULL)))
{
perror("Error creating temporary filename!");
abort();
}
 
fprintf(stdout, "Temporary pathname %s\n", tmp_pathname);
 
if (!(tmp_fileptr = fopen(tmp_pathname, "w")))
{
perror("Error opening temporary file");
abort();
}
else
{
fclose(tmp_fileptr);
remove(tmp_pathname);
}
 
/* - tmpfile - */
if (!(tmp_filestream = tmpfile()))
{
perror("Error generating temporary stream!");
abort();
}
 
/* write to temporary file */
fprintf(tmp_filestream, "Temporary stream created by PID[%ld]", (long)getpid());
fflush(tmp_filestream);
 
/* read from temporary file */
rewind(tmp_filestream);
fgets(buffer, 100, tmp_filestream);
fprintf(stdout, "Temporary stream: %s\n", buffer);
 
if(tmp_filestream)
fclose(tmp_filestream);
 
return 0;
}

Postconditions

Two temporary files are created. The tmpfile() created file is automatically deleted by the OS when all references to the file are closed.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fThemesE3aHomeE5fScreenX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ