一、使用文件IO完成对文件进行加密
#include <myhead.h>
int main(int argc, char const *argv[])
{
if(argc !=3)
{
write(2,"input file error\n",sizeof("input file error"));
return -1;
}
int srcfd=open(argv[1],O_RDONLY);
if(srcfd==-1)
{
perror("open srcfd error");
return -1;
}
int dstfd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0777);
if(dstfd==-1)
{
perror("open dstfd error");
return -1;
}
char buf[128]="";
while(1)
{
int src = read(srcfd,buf,sizeof(buf));
if(src == 0)
{
break;
}
//转换大小写
for(int i=0;i<strlen(buf);i++)
{
if(buf[i]>='A'&&buf[i]<='Z')
buf[i]+=32;
else if(buf[i]>='a'&&buf[i]<='z')
buf[i] -=32;
}
printf("运行成功\n");
write(dstfd,buf,src);
}
printf("加密成功!\n");
close(srcfd);
close(dstfd);
return 0;
}
思维导图: