linux实验报告二--文件操作
2006-03-09 10:28
477 查看
//利用底层文件操作函数编写一个mycopy.c程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define maxsize 256 //定义一次从文件读字符的最大数
main(int argc,char *argv[])
{
int fd1,fd2; //定义源文件和目的文件的文件描述符
char buff[maxsize]; //缓冲
int i;
if(argc!=3) //如果命令格式不正确
{ printf("command error!/n");
exit(1);
}
fd1=open(argv[1],O_RDONLY); //以只读的方式打开源文件
if(fd1==-1)
{ printf("file %s cannot open",argv[1]);
exit(1);
}
fd2=open(argv[2],O_WRONLY|O_CREAT|O_APPEND);//以追加的方式创建目的文件
if(fd2==-1)
{ printf("cannot creat file %s",argv[1]);
exit(1);
}
while(1)
{
i=read(fd1,buff,maxsize);
write(fd2,buff,i);
if(i!=maxsize) break; //如果读到的字节数不是希
//望的bufsize,结束文件读写
}
close(fd1);
close(fd2);
}
//使用流文件函数实现相同的功能
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define maxsize 5
main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char *ch;
if(argc!=3)
{ printf("command error!/n");
exit(1);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{ printf("file %s cannot open",argv[1]);
exit(1);
}
fp2=fopen(argv[2],"wa+");
if(fp2==NULL)
{ printf("cannot creat file %s",argv[1]);
exit(1);
}
while(1)
{
ch=fgets(ch,maxsize,fp1);
if(ch!="EOF")
fputs(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define maxsize 256 //定义一次从文件读字符的最大数
main(int argc,char *argv[])
{
int fd1,fd2; //定义源文件和目的文件的文件描述符
char buff[maxsize]; //缓冲
int i;
if(argc!=3) //如果命令格式不正确
{ printf("command error!/n");
exit(1);
}
fd1=open(argv[1],O_RDONLY); //以只读的方式打开源文件
if(fd1==-1)
{ printf("file %s cannot open",argv[1]);
exit(1);
}
fd2=open(argv[2],O_WRONLY|O_CREAT|O_APPEND);//以追加的方式创建目的文件
if(fd2==-1)
{ printf("cannot creat file %s",argv[1]);
exit(1);
}
while(1)
{
i=read(fd1,buff,maxsize);
write(fd2,buff,i);
if(i!=maxsize) break; //如果读到的字节数不是希
//望的bufsize,结束文件读写
}
close(fd1);
close(fd2);
}
//使用流文件函数实现相同的功能
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define maxsize 5
main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char *ch;
if(argc!=3)
{ printf("command error!/n");
exit(1);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{ printf("file %s cannot open",argv[1]);
exit(1);
}
fp2=fopen(argv[2],"wa+");
if(fp2==NULL)
{ printf("cannot creat file %s",argv[1]);
exit(1);
}
while(1)
{
ch=fgets(ch,maxsize,fp1);
if(ch!="EOF")
fputs(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}
相关文章推荐
- linux实验报告二--文件操作(续)
- 操作系统大型实验进展(6)-----Linux 内核文件系统与设备操作流程分析
- Linux实验报告-源代码编译安装Apache(Tarball文件安装)
- x4412开发板&ibox卡片电脑项目实战19-linux文件操作实验
- x4412开发板&ibox卡片电脑项目实战19-linux文件操作实验
- 【Linux】使用read、write、lseek函数实现文件读写操作
- Linux系统编程:简单文件IO操作
- linux文件操作指令
- Linux文件操作总结
- Linux成长之路(一)——基本概念及操作、用户及文件权限管理
- linux文件操作
- Linux中常用的批量操作文件命令
- Linux文件操作命令
- 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件
- Linux常见文件操作命令
- 3)Linux程序设计入门--文件操作
- Linux/Unix 下对文件操作的学习
- Linux 下几个文件操作命令的代码实现
- linux对文件操作命令集合
- Linux文件的相关操作命令