#include
#include
#include
//#include
#define FILE "test.txt"
void
create_file (int *fd)
{
*fd = creat (FILE, 0700);
printf ("--->%d.\n", *fd);
if (*fd == -1)
{
perror ("创建失败.\n");
return;
}
else
printf ("创建成功.\n");
printf ("--->%d.\n", *fd);
}
void
open_file (int fd)
{
int n = 0;
n = open (FILE, O_RDWR);
if (n == -1)
{
perror ("打开失败.\n");
return;
}
}
void
close_file (int fd)
{
int n = 0;
n = close (fd);
if (n == -1)
{
perror ("关闭文件失败.\n");
}
printf ("关闭文件成功.\n");
}
void
write_string (int fd, char *str)
{
if (fd == -1)
{
perror ("文件没有打开");
return;
}
int n = 0;
n = write (fd, str, strlen (str));
if (n == -1)
{
printf ("写入失败.\n");
return;
}
printf ("写入了%d个字.\n", n);
}
void
read_string (int fd)
{
char buf[1024];
int n = 0;
n = read (fd, buf, 1024);
if (n == -1)
{
printf ("读取失败.\n");
return;
}
printf ("读取了%d个字.\n", n);
}
void
copy_file (int fd)
{
int n = 0;
n = dup (fd);
if (n == -1)
{
perror ("复制文件失败.\n");
return;
}
printf ("复制文件成功.\n");
}
void
remove_file (char * str)
{
int n = 0;
n = remove (FILE);
}
int
main ()
{
int fd;
char *str = "hello world.\n";
create_file (&fd);
close_file (fd);
open_file (fd);
write_string (fd, str);
close_file (fd);
open_file (fd);
read_string (fd);
printf ("--->%d.\n", fd);
close_file (fd);
open_file (fd);
copy_file (fd);
printf ("--->%d.\n", fd);
close_file (fd);
remove_file (fd);
return 0;
}
阅读(1313) | 评论(0) | 转发(0) |