分类: C/C++
2010-10-25 20:35:42
/********************
创建文件
*********************/
#include
#include
#include
#include
#include
int main (void)
{
int fd;
fd = open("test", O_CREAT,S_IRUSR|S_IWUSR|S_IXGRP);
printf("fd=%d\n",fd);
return 0;
}
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
fd = open("test",O_CREAT,00750);
if(fd = -1)
{
perror("open error");
exit (1);
}
return 0;
}
/******************
文件复制程序
*******************/
#include
#include
#include
#include
#include
#include
#include
#define size 256
int main (int argc, char **argv)
{
int fd1, fd2;
int cntr,cntw;
char buffer[size];
char *ptr;
if (argc!=3)
{
printf ("too few arguments.\n");
exit(1);
}
fd1 = open(argv[1],O_RDONLY);
if(fd1==-1)
{
perror("open file1 failed");
exit (1);
}
fd2 = open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if(fd2==-1)
{
perror("open file2 failed");
exit (1);
}
while((cntr=read(fd1,buffer,size))!=0)
{
if(cntr==-1)
{
perror("read error");
exit (1);
}
else if(cntr>0)
{
ptr = buffer;
while((cntw=write(fd2,ptr,cntr))!=0)
{
if(cntw==-1)
{
perror("write error");
exit (1);
}
else if(cntr==cntw)
break;
else if(cntw>0)
{
ptr+=cntw;
cntr-=cntw;
}
}
if(cntw==-1)
break;
}
}
close (fd1);
close (fd2);
printf("Done!\n");
return 0;
}
/******************
创建文件的符号连接
*******************/
#include
#include
#include
#include
int main(int argc, char **argv)
{
int ret;
if(argc!=3)
{
printf("too few argumunts\n");
exit(1);
}
ret = symlink(argv[1],argv[2]);
if(ret==-1)
{
perror("syslink failed");
exit(1);
}
printf("done\n");
return 0;
}