#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen(argv[1],"r");
if(fp == NULL)
{
perror("fopen");
return -1;
}
char c;
while( fread(&c,1,sizeof(c),fp) !=0)
{
printf("%c",c);
}
fseek(fp,0,SEEK_END);
long a = ftell(fp);
printf("%ld\n",a);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
int fd = open(argv[1],O_RDWR);
int fd1 = open("open.txt",O_RDWR|O_CREAT,0777);
if(fd < 0||fd1<0)
{
perror("open");
return -1;
}
ssize_t len;
char arr[100] = "";
while(1)
{
bzero(arr,sizeof(arr));
len = read(fd,arr,sizeof(arr));
if(0 == len)
{
printf("文件读取完毕\n");
break;
}
else if(len<0)
{
perror("read");
break;
}
write(fd1,arr,len);
}
if(0 ==fd1)
{
perror("write");
}
if(close(fd)<0||close(fd1)<0)
{
perror("close");
return -1;
}
return 0;
}