1.
#include <stdio.h>
int main(int argc, const char *argv[])
{
if(argc<2)
{
printf("命令行未传参,请输入文件名\n");
return -1;
}
FILE* max = fopen(argv[1],"r");
if(NULL ==max)
{
perror("fopen");
return -1;
}
char c;
while(fread(&c,1,sizeof(c),max) == sizeof(c))
{
printf("%c",c);
}
fclose(max);
return 0;
2.
#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[])
{
//读取01main.c文件中的数据
int max = open("./01main.c", O_RDONLY);
if(max <0)
{
perror("open");
return -1;
}
//新建一个空的文件并给权限
int much = open("./put.txt",O_WRONLY | O_CREAT | O_TRUNC,0777);
if(max < 0)
{
perror("open");
return -1;
}
ssize_t len;
char arr[1024] = "";
//循环读取01main.c文件中的数据
while(1)
{
bzero(arr,sizeof(arr));
len = read(max,arr,sizeof(arr));
if(0 == len)
{
printf("文件读取完成\n");
break;
}
else if(0 > len)
{
printf("文件读取失败\n");
break;
}
write(much,arr,len);
}
if(close(max) <0)
{
perror("close");
return -1;
}
close(much);
return 0;
}