1.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
for(int num=1;num<argc;num++){
int out = open(argv[num],O_RDONLY),jump=1;
if(-1==out){
printf("%s\n",strerror(errno));
continue;
}
char data[20] = {0};
while(jump){
memset(data,0,sizeof(data));
jump = read(out,data,sizeof(data)-1);
printf("%s",data);
}
close(out);
printf("\n");
}
return 0;
}
2.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
if(argc!=3){
printf("传入参数非法!程序结束!\n");
return 0;
}
int file1 = open(argv[1],O_RDONLY);
if(-1==file1){
printf("%s:%s\n",argv[1],strerror(errno));
return 0;
}
int file2 = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0777),jump=1;
if(-1==file2){
printf("%s:%s\n",argv[2],strerror(errno));
return 0;
}
char data[20] = {0};
while(jump){
jump = read(file1,data,sizeof(data)-1);
write(file2,data,strlen(data));
memset(data,0,strlen(data));
}
close(file1);
close(file2);
return 0;
}
3.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
if(argc!=3){
printf("传入参数非法!程序结束!\n");
return 0;
}
int file1 = open(argv[1],O_RDONLY),jump1=1;
if(-1==file1){
printf("%s:%s\n",argv[1],strerror(errno));
return 0;
}
int file2 = open(argv[2],O_RDONLY),jump2=1;
if(-1==file2){
printf("%s:%s\n",argv[2],strerror(errno));
return 0;
}
char data1[20] = {0},data2[20] = {0};
while(jump1&&jump2){
//只要其中一个读完就结束循环
//但是一个结束另一个不结束就通过if跳出了
jump1 = read(file1,data1,sizeof(data1)-1);
jump2 = read(file2,data2,sizeof(data2)-1);
if(strcmp(data1,data2)){ //为0相等,不执行,继续循环
printf("%s 与 %s 不相同!\n",argv[1],argv[2]);
return 0;
}
memset(data1,0,sizeof(data1));
memset(data2,0,sizeof(data2));
}
printf("%s 与 %s 相同!\n",argv[1],argv[2]);
close(file1);
close(file2);
return 0;
}