linux文件编程-系统调用
2013-03-28 13:26
344 查看
linux编程-系统调用(systm-call)
1、创建文件:
int creat(const char *pathname,mode_t mode)
实例代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void creat_file(char *filename)
{
if(creat(filename,0775)<0)
{
printf("creat file %s failure!!",filename);
exit(EXIT_FAILURE);
}
else
{
printf("creat file %s success!\n",filename);
}
}
int main(int argc,char *argv[])
{
int i;
if(argc==1)
{
perror("you haven't input the filename,please input again");
exit(EXIT_FAILURE);
}
for(i=1;i<argc;i++)
{
creat_file(argv[i]);
}
exit(EXIT_SUCCESS);
}
2、打开文件
int open(const char*pathname,int flag,mode_t mode)
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int fd;
if(argc<2)
{
puts("please input the open file pathname!\n");
exit(1);
}
if((fd=open(argv[1],O_CREAT | O_RDONLY,0700))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %s success!\n",fd);
}
close(fd);
exit(0);
}
3、文件的读写操作
int read(int fd,const void *buf,size_t length)
int write(int fd,const void *buf,size_t length)
综述实例:完成文件内容的cope.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile\n\a",argv[0]);
exit(1);
}
/* 打开源文件 */
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}
/* 创建目的文件 */
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
/* 以下代码是一个经典的拷贝文件的代码 */
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
/* 一个致命的错误发生了 */
if((bytes_read==-1)&&(errno!=EINTR)) break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
/* 一个致命错误发生了 */
if((bytes_write==-1)&&(errno!=EINTR))break;
/* 写完了所有读的字节 */
else if(bytes_write==bytes_read) break;
/* 只写了一部分,继续写 */
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
/* 写的时候发生的致命错误 */
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
1、创建文件:
int creat(const char *pathname,mode_t mode)
实例代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void creat_file(char *filename)
{
if(creat(filename,0775)<0)
{
printf("creat file %s failure!!",filename);
exit(EXIT_FAILURE);
}
else
{
printf("creat file %s success!\n",filename);
}
}
int main(int argc,char *argv[])
{
int i;
if(argc==1)
{
perror("you haven't input the filename,please input again");
exit(EXIT_FAILURE);
}
for(i=1;i<argc;i++)
{
creat_file(argv[i]);
}
exit(EXIT_SUCCESS);
}
2、打开文件
int open(const char*pathname,int flag,mode_t mode)
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int fd;
if(argc<2)
{
puts("please input the open file pathname!\n");
exit(1);
}
if((fd=open(argv[1],O_CREAT | O_RDONLY,0700))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %s success!\n",fd);
}
close(fd);
exit(0);
}
3、文件的读写操作
int read(int fd,const void *buf,size_t length)
int write(int fd,const void *buf,size_t length)
综述实例:完成文件内容的cope.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile\n\a",argv[0]);
exit(1);
}
/* 打开源文件 */
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}
/* 创建目的文件 */
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
/* 以下代码是一个经典的拷贝文件的代码 */
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
/* 一个致命的错误发生了 */
if((bytes_read==-1)&&(errno!=EINTR)) break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
/* 一个致命错误发生了 */
if((bytes_write==-1)&&(errno!=EINTR))break;
/* 写完了所有读的字节 */
else if(bytes_write==bytes_read) break;
/* 只写了一部分,继续写 */
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
/* 写的时候发生的致命错误 */
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
相关文章推荐
- Linux文件编程之【系统调用】——open()
- Linux文件编程(系统调用1)
- Linux文件编程之--库函数pk系统调用函数
- 文件编程之Linux下系统调用
- Linux文件编程,时间编程——系统调用-文件访问
- Linux下系统调用文件编程整理
- Linux文件编程--系统调用
- linux系统编程之文件与IO(四):目录访问相关系统调用
- 【Linux系统调用】文件编程
- Linux系统编程——系统调用之 I/O 操作(文件操作)
- Linux文件编程之【系统调用】——close()
- Linux文件编程之【系统调用】——read()
- Linux文件编程之【系统调用】——write()
- Linux 系统调用文件编程(文件操作)
- linux应用程序开发-文件编程-系统调用方式
- Linux文件编程之【系统调用】—— lseek()
- LS8-linux系统调用方式文件编程之学习笔记
- linux历程--文件编程(系统调用)
- Linux文件编程之【系统调用】—— access()
- Linux系统编程_2_文件I/O(系统调用&库函数)