Ò»ÇÐÖ»ÊÇ¿ªÊ¼
来源: BlogBus 原始链接: http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=154403 存档链接: https://web.archive.org/web/20040624190134id_/http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=154403
Ò»ÇÐÖ»ÊÇ¿ªÊ¼ Linux, ǶÈëʽϵͳ, ´®¿ÚͨÐÅ, PALM OS, Bluetooth ... Êǹ¤×÷ÖÐÕýÓõ½µÄ
Java, software engineering, ÒÔ¼°ÆäËûµÄºÜ¶à¼¼Êõ£¬ÊÇÎÒ¸ÐÐËȤµÄ <<<¶ÔÓÚ sizeof µÄһЩÀí½â | Ê×Ò³ | ¹ØÓÚ fork ºÍ¸¸×Ó½ø³ÌµÄÀí½â>>> Linux Ï´´½¨ÁÙʱÎļþ ʱ¼ä: 04/04/26 Carol noted from << Beginning Linux Programming >> Chap 4 ÓÐʱºò³ÌÐòÐèҪһЩÎļþÐÎʽµÄÁÙʱ´æ´¢£¬À´±£ÁôһЩÖмä¹ý³ÌµÄ²Ù×÷¡£Èç¹û°´ÕÕÒ»°ã·½Ê½´´½¨ÁÙʱÎļþ£¬¿ÉÄÜ»áÃæÁÙÖØÃûµÄÎÊÌ⣬µ¼ÖÂһЩ³åÍ»¡£¿ÉÒÔʹÓà tmpname() º¯ÊýÀ´Éú³ÉÒ»¸öÎļþÃû£¬±ÜÃâÕâÖÖÇé¿ö¡£ #include < stdio.h
char *tmpnam(char *s); tmpnam ·µ»ØÒ»¸ö²»ÓëÒÑÓÐÎļþÖØ¸´µÄÎļþÃû¡£ Èç¹ûÁÙʱÎļþ´´½¨ÒÔºóÂíÉÏʹÓ㬿ÉÒÔʹÓà tmpfile º¯ÊýÀ´Í¬Ê±Íê³ÉÃüÃûºÍ´ò¿ª¡£ #include < stdio.h
FILE *tmpfile(void); Tmpfile º¯Êý·µ»ØÎļþÁ÷Ö¸Õ루 FILE * £© , Ö¸ÏòÉú³ÉµÄÁÙʱÎļþ¡£Îļþ¿É¶Á¿Éд£¨Ê¹Óà fopen, w+ £©¶øÇÒÔÚËùÓÐÒýÓýáÊøµÄʱºò×Ô¶¯É¾³ý¡£ Àý³Ì£º #include <stdio.h> int main() { char tmpname[L_tmpnam]; char *filename; FILE *tmpfp; filename = tmpnam(tmpname); printf("Temporary file name is: %s\n", filename); tmpfp = tmpfile(); if(tmpfp) printf("Opened a temporary file OK\n"); else perror("tmpfile"); exit(0); } When we compile and run this program, tmpnam.c, we can see the unique file name generated by tmpnam: $ ./tmpnam Temporary file name is: /tmp/filedm9aZK Opened a temporary file OK ¸ü¶à¹ØÓÚ´´½¨ÁÙʱÎļþµÄÌÖÂÛ Temporary Files Often, programs will need to make use of temporary storage in the form of files. These might hold intermediate results of a computation, or might represent backup copies of files made before critical operations. For example, a database application could use a temporary file when deleting records. The file collects the database entries that need to be retained and then, at the end of the process, the temporary file becomes the new database and the original is deleted. This popular use of temporary files has a hidden disadvantage. You must take care to ensure that they choose a unique file name to use for the temporary file. If this doesn't happen, because UNIX is a multitasking system, another program could choose the same name and the two will interfere with each other. A unique file name can be generated by the tmpnam function: #include < stdio.h
char *tmpnam(char *s); The tmpnam function returns a valid file name that isn't the same as any existing file. If the string s isn't null, the file name will also be written to it. Further calls to tmpnam will overwrite the static storage used for return values, so it's essential to use a string parameter if tmpnam is to be called many times. The string is assumed to be at least L_tmpnam characters long. tmpnam can be called up to TMP_MAX times in a single program and it will generate a different file name each time. If the temporary file is to be used immediately, you can name it and open it at the same time using the tmpfile function. This is important, since another program could create a file with the same name as that returned by tmpnam. The tmpfile function avoids this problem altogether: #include < stdio.h
FILE *tmpfile(void); The tmpfile function returns a stream pointer that refers to a unique temporary file. The file is opened for reading and writing (via fopen with w+) and it will be automatically deleted when all references to the file are closed. tmpfile returns a null pointer and sets errno on error. Try It Out - tmpnam and tmpfile Let's see these two functions in action: #include <stdio.h> int main() { char tmpname[L_tmpnam]; char *filename; FILE *tmpfp; filename = tmpnam(tmpname); printf("Temporary file name is: %s\n", filename); tmpfp = tmpfile(); if(tmpfp) printf("Opened a temporary file OK\n"); else perror("tmpfile"); exit(0); } When we compile and run this program, tmpnam.c, we can see the unique file name generated by tmpnam: $ ./tmpnam Temporary file name is: /tmp/filedm9aZK Opened a temporary file OK How It Works The program calls tmpnam to generate a unique file name for a temporary file. If we wanted to use it, we would have to open it quickly to minimize the risk that another program would open a file with the same name. The tmpfile call creates and opens a temporary file at the same time, thus avoiding this risk. Older versions of UNIX have another way to generate temporary file names using functions mktemp and mkstemp . These are similar to tmpnam, except that you can specify a template for the temporary file name, which gives you a little more control over their location and name: #include < stdlib.h
char *mktemp(char *template); int mkstemp(char *template); The mktemp function creates a unique file name from the given template. The template argument must be a string with six trailing X characters. The mktemp function replaces these X characters with a unique combination of valid file name characters. It returns a pointer to the generated string, or a null pointer if it couldn't generate a unique name. The mkstemp function is similar to tmpfile in that it creates and opens a temporary file. The file name is generated in the same way as mktemp, but the returned result is an open, low-level, file descriptor. In general, you should use tmpnam and tmpfile rather than mktemp and mkstemp. carol ·¢±íÓÚ 04/04/26 22:27 ÒýÓÃ(Trackback0) ÆÀÂÛ Ñ§ÁËÒ»ÕУ¬»¹ÓÐtmpnameÕ⺯Êý°¡ zhou ( zhouxiaohu.blogbus.com ) ·¢±íÓÚ 04/04/27 20:44 ·¢±íÆÀÂÛ ×îºó¸üР[תÌù]ÕæµÄÁ˽âtelnetÂ𣿠[ͼ]Introduction to Bluetooth Record - June 15th~ [WIN CE] Ô¶³Ì¿ØÖÆ£¬Ê°빦±¶ ´Ó¿ª·¢½Ç¶È¿´ smartphone palm ÉÏÎå·N²»Í¬µÄ reset [ת] ·¨Óï The 11 Truths of Debugging [תÌù]΢ÈíSmartphoneÓ¦ÓóÌÐòÉè¼ÆÈëÃÅ smartphone Ò»ÀÀ