博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞
creatory.cublog.cn


Unix Advanced Programming Practise
稀疏文件
0 0 0 0 1
0 0 0 2 0
0 0 3 0 0
0 4 0 0 0
5 0 0 0 0
例:创建一个稀疏文件(可以优化存储)
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main(void)
{
int fd;
if((fd=open("./test",O_CREAT|O_WRONLY|O_TRUNC,S_IWUSR|S_IRUSR))==-1)
{
printf("open error:%s\n",strerror(errno));
exit(1);
}
if((lseek(fd,1024*1024*1024,SEEK_SET))==(off_t)-1)
{
printf("lseek error:%s\n",strerrror(errno));
exit(1);
}
if((write(fd,"EndofFile",9))==-1)
{
printf("write error:%s\n",strerror(errno));
exit(1);
}
close(fd);
return 0;
}
该程序创建了一个1GB多的文件并在未尾写入了9个字符.
用ls -l列一下会看到其大小,这样会造成空间的浪费.
强迫将缓冲区的数据写入磁盘
<unistd.h>
void sync(void);
例:
#include <unistd.h>
#include <stdio.h>
int main(void)
{
sync();
printf("sync execute success,see your hard disk lamp!\n");
return 0;
}
将范围限制到一个文件
<unistd.h>
int fsync(int fd);
分散读写
将多个缓冲区的数据进行合并读写
<sys/types.h><sys/uio.h><unistd.h>
ssize_t readv(int fd,const struct iovec *iov,int iovcnt);
ssize_t writev(int fd,const struct iovec *iov,int iovcnt);
struct iovec
{
char *iov_base;
size_t iov_len;
}
例:将四个缓冲区的数据合并读写
#include XXXX
int main()
{
int z;
char buf1[]="am";
char buf2[]="creatoy";
char buf3[]="I";
char buf4[]="\n";
struct iovec iov[4];
iov[0].iov_base=buf3;
iov[0].iov_len=strlen(buf3);
iov[1].iov_base=buf1;
iov[1].iov_len=strlen(buf1);
iov[2].iov_base=buf2;
iov[2].iov_len=strlen(buf2);
iov[3].iov_base=buf4;
iov[3].iov_len=strlen(buf4);
z=write(1,&iov[0],4);
if(z==-1)
abort();
return 0;
}
输出结果应为:
Iamcreatory
确定终端tty名称
<unistd.h>
char *ttyname(int fd);
int isatty(int fd);
测试程序:
#include <stdio.h>
#include <unistd.h>
void tty_judge(int fd)
{
int z=isatty(fd);
printf("%fd=%d,%s a atty",fd,z?"is":"isn't");
if(z)
printf("tty name:%s\n",ttyname(fd);
}
int main()
{
int i;
for(i=0;i<5;i++)
tty_judge(i);
return 0;
}
清除文件
<unistd.h>
int unlink(const char *pathname);

<stdio.h>
int remove(const char *path);可以清除空目录
例:删除文件的程序
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,char **argv)
{
if(argc!=2)
{
printf("Usage:delte file\n");
exit(1);
}
if(unlink(argv[1])==-1)
printf("unlink error:%s\n",strerror(errno));
else
printf("unlink file success\n");
return 0;
}
硬链接文件
<unistd.h>
int link(const char *oldpath,const char *newpath);
例:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("Usage:link old new\n");
exit(1);
}
errno=0;
if(link(argv[1],argv[2])==-1)
printf("link error:%s\n"strerror(errno)):
else
printf("link success:%s---->>%s\n",argv[2],argv[1]);
return 0;
}
移动文件
思路:先用link做硬链接,再用unlink删除旧文件,这样只留下了新的文件
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("Usage:mv old new\n");
exit(1);
}
if(link(argv[1],argv[2])==-1)
{
printf("link error:%s\n",strerror(errno));
exit(1);
}
if(unlink(argv[1])==-1)
{
printf("unlink error:%s\n",strerror(errno));
exit(1);
}
printf("mv success:%s---->>>%s\n",argv[1],argv[2]);
return 0;
}

发表于: 2008-03-25 ,修改于: 2008-03-25 14:08,已浏览122次,有评论0条 推荐 投诉


网友评论

发表评论