| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> 在linux下使用c语言操作临时文件 zai linux xia shi yong c yu yan cao zuo lin shi wen jian
 

在linux下使用c语言操作临时文件

2008-06-01 02:05:29 编辑來源:互联网 繁體版 评论
 
 
本文为【在linux下使用c语言操作临时文件】的汉字拼音对照版显示拼音
  /******************************************************************

  *benwenshoufayubbs.bluegem.orgdeCWorldqu

  *benrenemailchenfei@sohu.com

  *ruzhuanzaibenwenqingbaoliushoufadihebenrenlianluofangshiyifang便bianjiaoliu

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  xiexie

  ******************************************************************/

   youshichengxuxuyaocunchuhendaliangdeshujuhuozhezaijigejinchengjianjiaohuanshujuzheshininkenengkaolvdao使shiyonglinshiwenjian使shiyonglinshiwenjianyaokaolvjigewenti

  1baozhenglinshiwenjianjiandewenjianmingbuhuzhuchongtu

  2baozhenglinshiwenjianzhongneirongbubeiqitayonghuhuozheheiketoukanshanchuhexiugai

  suoyizailinuxxiayouzhuanmenchulilinshiwenjiandehanshu

  mkstemphanshu

   mkstemphanshujiangzaixitongzhongyiduyiwuerdewenjianmingchuangjianyigewenjianbingdakaierqiezhiyoudangqianyonghucaiyou访fangwenzhegelinshiwenjiandequanxiandangqianyonghuduizhegelinshiwenjiankeyidakaibingjinxingduxiecaozuomkstemphanshuzhiyouyigeshenshuzhegeshenshushigeyiXXXXXXjieyidefeikongzifuchuanmkstemphanshuhuiyongsuijichanshengdezifuchuantihuanXXXXXXbaozhenglewenjianmingdeweiyixinghanshufanhuiyigewenjianmiaoshufujiaruzhixingshibaifanhui-1zaiglibc 2.0.6 yijigengzaodeglibckuzhongzhegewenjiande访fangwenquanxianshi0666 ,glibc 2.0.7 yihoudekuzhegewenjiande访fangwenquanxianshi0600

   danglinshiwenjianwanchengtade使shimingjiarububataqingchuganjingbahuozhechengxuyouyuyiwaizailinshiwenjianbeiqingchuqianjiuyijing退tuichulinshiwenjiansuozaidemuluhuisaimanlajiyouyumkstemphanshuchuangjiandelinshiwenjianbunengzidongshanchuqingshenkaoxiawenzhongdetmpfilehanshuzhixingwanmkstemphanshuhouyaotiaoyongunlinkhanshuunlinkhanshushanchuwenjiandemulurukousuoyilinshiwenjianhuankeyitongguowenjianmiaoshufujinxing访fangwenzhidaozuihouyigedakaidejinchengguanbiwenjiancaozuofuhuozhechengxu退tuichuhoulinshiwenjianbeizidongchedidishanchu

  licheng

  zhijie使shiyongadvanced linux PRogrammingdelichengzhibazhushifanyiyixia

  #include <stdlib.h>

  #include <unistd.h>

  /* A handle for a temporary file created with write_temp_file. In

  this implementation, its just a file descriptor. */

  /*write_temp_fileshigecaozuolinshiwenjiandejubingbenlizhongzhishigewenjianmiaoshufu*/

  typedef int temp_file_handle;

  /* Writes LENGTH bytes from BUFFER into a temporary file. The

  temporary file is immediately unlinked. Returns a handle to the

  temporary file. */

  /*zaizhehanshucongBUFFERzhongxianglinshiwenjianxieruLENGTHzijieshujulinshiwenjianzaigangyichuangjianjiubeishanchudiaohanshuhuifanhuilinshiwenjiandejubing*/

  temp_file_handle write_temp_file (char* buffer, size_t length)

  {

  /* Create the filename and file. The XXXXXX will be replaced with

  characters that make the filename unique. */

  /*xinjianwenjianminghewenjianwenjianmingzhongdeXXXXXXjiangbeisuijizifuchuandaitiyibaozhengwenjianmingzaixitongzhongdeweiyixing*/

  char temp_filename[] = /tmp/temp_file.XXXXXX;

  int fd = mkstemp (temp_filename);

  /* Unlink the file immediately, so that it will be removed when the

  file descriptor is closed. */

  /*wenjianmashangbeiunlinkzheyangzhiyaowenjianmiaoshufuyiguanbiwenjianjiuhuibeizidongshanchu*/

  unlink (temp_filename);

  /* Write the number of bytes to the file first. */

  /*shouxianxierujijiangxierushujudechangdu*/

  write (fd, &length, sizeof (length));

  /* Now write the data itself. */

  /*xierushujubenshen*/

  write (fd, buffer, length);

  /* Use the file descriptor as the handle for the temporary file. */

  /*hanshufanhuiwenjianmiaoshufuzuoweilinshiwenjiandejubing*/

  return fd;

  }

  /* Reads the contents of a temporary file TEMP_FILE created with

  write_temp_file. The return value is a newly allocated buffer of

  those contents, which the caller must deallocate with free.

  *LENGTH is set to the size of the contents, in bytes. The

  temporary file is removed. */

  /*congbeiwrite_temp_filechuangjiandelinshiwenjianzhongduqushujufanhuizhishihanyouwenjianneirongdexinshenqingdaodeneicunkuaizhekuaineicunyinggaiyoutiaoyongread_temp_filezheshifang

  *lengthshilinshiwenjianzhengwenneirongdechangduzhixingwanread_temp_filehanshuhoulinshiwenjianbeichedishanchu*/

  char* read_temp_file (temp_file_handle temp_file, size_t* length)

  {

  char* buffer;

  /* The TEMP_FILE handle is a file descriptor to the temporary file. */

  /*fdshi访fangwenlinshiwenjiandewenjianmiaoshufu*/

  int fd = temp_file;

  /* Rewind to the beginning of the file. */

  /*bawenjianzhizhenzhixiangwenjiankaitou*/

  lseek (fd, 0, SEEK_SET);

  /* Read the size of the data in the temporary file. */

  /*huodelinshiwenjianzhengwenchangdu*/

  read (fd, length, sizeof (*length));

  /* Allocate a buffer and read the data. */

  /*fenpeineicunkuaiduqushuju*/

  buffer = (char*) malloc (*length);

  read (fd, buffer, *length);

  /* Close the file descriptor, which will cause the temporary file to

  go away. */

  /*guanbiwenjianmiaoshufulinshiwenjianbeichedishanchu*/

  close (fd);

  return buffer;

  }

  

  

  tmpfilehanshu

   jiarunin使shiyongC library I/Ohanshubingqiebingmeiyoulingyigechengxu使shiyongzhegelinshiwenjianbizhezhuanwodelijieshizaitongyijinchenghuojuyoufuziguanxidejinchengzuzhongyougegengjianjiedehanshutmpfiletmpfilehanshuchuangjianbingdakaiyigelinshiwenjianbingqiezidongzhixingleunlinklezhegelinshiwenjiantmpfilehanshufanhuiyigewenjianmiaoshufujiaruzhixingshibaifanhuiNULLdangchengxuzhixinglefclosehuozhe退tuichushiziyuanbeishifang

   linuxxitongzhonghuantigongmktemp tmpnam hetempnamdenghanshudanshiyouyujianzhuangxingheanquanfangmianliyoubujianyi使shiyongtamen

在linux下使用c语言操作临时文件
gengduoneirongqingkanC/C++jinjiejishuwendang C++biancheng Linuxwenjianxiangguanwenzhangzhuantihuo原文】【拼音
 
 
 
 
上一篇《C++中对浮点数的格式化显示》
下一篇《C++数据结构学习:二叉树(4)》
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
/****************************************************************** *本文首发于bbs.bluegem.org的CWorld区 *本人email:chenfei@sohu.com *如转载本文,请保留首发地和本人联络方式,以方便交流, 谢谢! ******************************************************************/ 有时程序需要存储很大量的数据,或者在几个进程间交换数据,这时您可能考虑到使用临时文件。使用临时文件要考虑几个问题: 1、保证临时文件间的文件名不互助冲突。 2、保证临时文件中内容不被其他用户或者黑客偷看、删除和修改。 所以在linux下有专门处理临时文件的函数 mkstemp函数 mkstemp函数将在系统中以独一无二的文件名创建一个文件并打开,而且只有当前用户才有访问这个临时文件的权限,当前用户对这个临时文件可以打开并进行读、写操作。mkstemp函数只有一个参数,这个参数是个以“XXXXXX”结尾的非空字符串。mkstemp函数会用随机产生的字符串替换“XXXXXX”,保证了文件名的唯一性。函数返回一个文件描述符,假如执行失败返回-1。在glibc 2.0.6 以及更早的glibc库中这个文件的访问权限是0666 ,glibc 2.0.7 以后的库这个文件的访问权限是0600。 当临时文件完成她的使命假如不把它清除干净把或者程序由于意外在临时文件被清除前就已经退出,临时文件所在的目录会塞满垃圾。由于mkstemp函数创建的临时文件不能自动删除(请参考下文中的tmpfile函数)。执行完mkstemp函数后要调用unlink函数,unlink函数删除文件的目录入口,所以临时文件还可以通过文件描述符进行访问,直到最后一个打开的进程关闭文件操作符,或者程序退出后临时文件被自动彻底地删除。 例程: 直接使用advanced linux PRogramming的例程,只把注释翻译一下 #include <stdlib.h> #include <unistd.h> /* A handle for a temporary file created with write_temp_file. In this implementation, it’s just a file descriptor. */ /*write_temp_file是个操作临时文件的句柄,本例中只是个文件描述符*/ typedef int temp_file_handle; /* Writes LENGTH bytes from BUFFER into a temporary file. The temporary file is immediately unlinked. Returns a handle to the temporary file. */ /*在这函数从BUFFER中向临时文件写入LENGTH字节数据。临时文件在刚一创建就被删除掉。函数会返回临时文件的句柄。*/ temp_file_handle write_temp_file (char* buffer, size_t length) { /* Create the filename and file. The XXXXXX will be replaced with characters that make the filename unique. */ /*新建文件名和文件,文件名中的XXXXXX将被随机字符串代替,以保证文件名在系统中的唯一性*/ char temp_filename[] = “/tmp/temp_file.XXXXXX”; int fd = mkstemp (temp_filename); /* Unlink the file immediately, so that it will be removed when the file descriptor is closed. */ /*文件马上被unlink,这样只要文件描述符一关闭文件就会被自动删除*/ unlink (temp_filename); /* Write the number of bytes to the file first. */ /*首先写入即将写入数据的长度*/ write (fd, &length, sizeof (length)); /* Now write the data itself. */ /*写入数据本身*/ write (fd, buffer, length); /* Use the file descriptor as the handle for the temporary file. */ /*函数返回文件描述符,作为临时文件的句柄*/ return fd; } /* Reads the contents of a temporary file TEMP_FILE created with write_temp_file. The return value is a newly allocated buffer of those contents, which the caller must deallocate with free. *LENGTH is set to the size of the contents, in bytes. The temporary file is removed. */ /*从被write_temp_file创建的临时文件中读取数据。返回值是含有文件内容的新申请到的内存块,这块内存应该又调用read_temp_file者释放。 *length是临时文件正文内容的长度。执行完read_temp_file函数后临时文件被彻底删除*/ char* read_temp_file (temp_file_handle temp_file, size_t* length) { char* buffer; /* The TEMP_FILE handle is a file descriptor to the temporary file. */ /*fd是访问临时文件的文件描述符*/ int fd = temp_file; /* Rewind to the beginning of the file. */ /*把文件指针指向文件开头*/ lseek (fd, 0, SEEK_SET); /* Read the size of the data in the temporary file. */ /*获得临时文件正文长度*/ read (fd, length, sizeof (*length)); /* Allocate a buffer and read the data. */ /*分配内存块,读取数据*/ buffer = (char*) malloc (*length); read (fd, buffer, *length); /* Close the file descriptor, which will cause the temporary file to go away. */ /*关闭文件描述符,临时文件被彻底删除*/ close (fd); return buffer; } tmpfile函数 假如您使用C library I/O函数,并且并没有另一个程序使用这个临时文件(笔者注:按我的理解是在同一进程或具有父子关系的进程组中),有个更简洁的函数——tmpfile。tmpfile函数创建并打开一个临时文件,并且自动执行了unlink了这个临时文件。tmpfile函数返回一个文件描述符,假如执行失败返回NULL。当程序执行了fclose或者退出时,资源被释放。 linux系统中还提供mktemp、 tmpnam、 和tempnam等函数,但是由于健壮性和安全方面理由不建议使用他们。 [url=http://www.wangchao.net.cn/bbs/hydetail_1785351.html][img]http://image.wangchao.net.cn/it/1323423798211.gif[/img][/url] 更多内容请看C/C++进阶技术文档 C++编程 Linux文件相关文章专题,或
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有