Linux文件编程综合(创建打开等)

include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#define FLAGS O_RDWR
#define MODE S_IRUSR|S_IWUSR|S_IXUSR
//创建文件函数
void write_creat()
{
const char *pathname;
char pn1[20];
int fd1;
printf("input:");
gets(pn1);
pathname=pn1;
if((fd1=creat(pathname,MODE))==-1)

{

printf("error!\n");
exit(1);
}
printf("OK!\n");
printf("fd1=%d\n",fd1);
return fd1;
}


//打开函数
void write_open()
{
const char *pathname;
char pn2[20];
int fd2;
printf("input:");
gets(pn2);
pathname=pn2;
if((fd2=open(pathname,FLAGS,MODE))==-1)
{
printf("error!\n");
exit(1);

}
printf("OK!\n");
printf("fd2=%d\n",fd2);
return fd2;
}


//关闭函数
void write_close()
{
const char *pathname;
int fd3;
char pn3[20];
printf("input:");
gets(pn3);
pathname=pn3;
if((fd3=close(pn3))==-1)
{
printf("error!\n");
exit(1);
}
printf("OK!\n");

printf("fd3=%d\n",fd3);
return fd3;
}


//读文件函数
void write_read()
{
int fd4;
const char *pathname;
int count;
int SIZE;
char read_buf[SIZE];
char pn4[20];
printf("input:");
gets(pn4);
pathname=pn4;
if((fd4=open(pathname,FLAGS,MODE))==-1)
{
printf("error!\n");
exit(1);
}

printf("OK!\n");
printf("began!\n");
printf("input size:");
gets(SIZE);
count=strlen(read_buf);
if((fd4=read(fd4,read_buf,count))==-1)
{
printf("error!\n");
exit(1);
}
printf("OK!\n");
printf("fd4=%d\n",fd4);
return fd4;
}


//写函数
void write_write()
{
int fd5;
const char *pathname;
int count;

int SIZE;
char write_buf[SIZE];
char pn5[20];
printf("input:");
gets(pn5);
pathname=pn5;
if((fd5=open(pathname,FLAGS,MODE))==-1)
{
printf("error!\n");
exit(1);
}
printf("OK!\n");
printf("began!\n");
printf("input size:");
gets(SIZE);
count=strlen(write_buf);
if((fd5=write(fd5,write_buf,count))==-1)
{
printf("error!\n");
exit(1);
}

}
printf("OK!\n");
printf("fd5=%d\n",fd5);
return fd5;
}


//主函数
void main()
{
write_creat();
write_open();
write_close();
write_read();
write_write();
printf("done!!!");
}
           
  



你可能感兴趣的:(Linux文件编程综合(创建打开等))