int main(int argc, const char *argv[])
{
DIR* dir1=opendir(argv[1]);
if(dir1==NULL){
perror("opendir");
return -1;
}
DIR* dir2=opendir(argv[2]);
if(dir2==NULL){
mkdir(argv[2],0771);
perror("mkdir");
}
struct dirent* file_name;
while(1){
file_name=readdir(dir1);
if(file_name==NULL){break;}
if(file_name->d_type==4){continue;}
char str1[255];
char str2[255];
strcpy(str1,argv[1]);
strcpy(str2,argv[2]);
strcat(str1,"/");
strcat(str2,"/");
strcat(str1,file_name->d_name);
strcat(str2,file_name->d_name);
printf("%s\n",str2);
int fd1=open(str1,O_RDONLY);
if(fd1==-1){
perror("open1");
return -3;
}
int fd2=open(str2,O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd2==-1){
perror("open2");
return -3;
}
char ch;
while(1){
int res=read(fd1,&ch,1);
if(res==0){break;}
write(fd2,&ch,1);
}
close(fd1);
close(fd2);
}
closedir(dir1);
closedir(dir2);
return 0;
}
#include "head.h"
int main(int argc, const char *argv[])
{
Inf_stu* H=cre_link();
while(1){
int n;
printf("please enter a number: ");
scanf("%d",&n);
switch(n){
case 1:{
Stu data;
printf("please enter the student name,age,score..: ");
scanf("%s",data.name);
scanf("%d",&data.age);
scanf("%lf",&data.chinese_score);
scanf("%lf",&data.math_score);
scanf("%lf",&data.english_score);
scanf("%lf",&data.phtsical_score);
scanf("%lf",&data.chemical_score);
scanf("%lf",&data.biological_score);
stu_in(H,data);
break;
}
case 2:
show(H);
break;
case 3:
write_file(H,argv[1]);
break;
case 4:
read_file(H,argv[1]);
break;
}
}
return 0;
}
#include"head.h"
//pointer way
Inf_stu* cre_link(){
Inf_stu* H=(Inf_stu*)malloc(sizeof(Inf_stu));
if(H==NULL){
perror("cre_link");
return NULL;
}
H->stu[50];
H->len=0;
return H;
}
//information input
void stu_in(Inf_stu* H,Stu data){
if(H->len>=sizeof(H->stu)/sizeof(Stu)){
printf("the link is full");
return;
}
H->stu[H->len]=data;
H->len++;
}
//show link information
void show(Inf_stu* H){
int Len=0;
while(Len<H->len){
printf("%d. ",Len+1);
printf("name:%s ",H->stu[Len].name);
printf("age:%d\n",H->stu[Len].age);
printf("chinese_score:%.2lf ",H->stu[Len].chinese_score);
printf("math_score:%.2lf ",H->stu[Len].math_score);
printf("english_score:%.2lf\n",H->stu[Len].english_score);
printf("phtsical_score:%.2lf ",H->stu[Len].phtsical_score);
printf("chemical_score:%.2lf ",H->stu[Len].chemical_score);
printf("biological_score:%.2lf\n",H->stu[Len].biological_score);
Len++;
}
}
//way of fprintf/fscanf
//input file
void input_file(Inf_stu* H,const char* file_name){
FILE* fp=fopen(file_name,"w");
if(fp==NULL){
perror("fopen");
return;
}
int Len=0;
while(Len<=H->len){
fprintf(fp,"%s ",H->stu[Len].name);
fprintf(fp,"%d ",H->stu[Len].age);
fprintf(fp,"%lf ",H->stu[Len].chinese_score);
fprintf(fp,"%lf ",H->stu[Len].math_score);
fprintf(fp,"%lf ",H->stu[Len].english_score);
fprintf(fp,"%lf ",H->stu[Len].phtsical_score);
fprintf(fp,"%lf ",H->stu[Len].chemical_score);
fprintf(fp,"%lf\n",H->stu[Len].biological_score);
Len++;
}
fclose(fp);
}
//output file
void output_file(Inf_stu* H,const char* file_name){
FILE* fp=fopen(file_name,"r");
if(fp==NULL){
perror("fopen");
return;
}
H->len=0;
while(1){
int res=fscanf(fp,"%s",H->stu[H->len].name);
if(res<=0){break;}
fscanf(fp,"%d",&(H->stu[H->len].age));
fscanf(fp,"%lf",&(H->stu[H->len].chinese_score));
fscanf(fp,"%lf",&(H->stu[H->len].math_score));
fscanf(fp,"%lf",&(H->stu[H->len].english_score));
fscanf(fp,"%lf",&(H->stu[H->len].phtsical_score));
fscanf(fp,"%lf",&(H->stu[H->len].chemical_score));
fscanf(fp,"%lf",&(H->stu[H->len].biological_score));
H->len++;
}
H->len--;
fclose(fp);
}
//way of fwrite/fread
//input file
void fwrite_file(Inf_stu* H,const char* file_name){
FILE* fp=fopen(file_name,"w");
if(fp==NULL){
perror("fopen");
return;
}
int res=fwrite(H,sizeof(Inf_stu),H->len,fp);
fclose(fp);
}
//output_file
void fread_file(Inf_stu* H,const char* file_name){
FILE* fp=fopen(file_name,"r");
if(fp==NULL){
perror("fopen");
return;
}
fread(H,sizeof(Inf_stu),50,fp);
fclose(fp);
}
//way of read/write
//input_file
void write_file(Inf_stu* H,const char* file_name){
int fd=open(file_name,O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd==-1){
perror("open");
return;
}
write(fd,H,sizeof(Inf_stu)*(H->len));
close(fd);
}
//output_file
void read_file(Inf_stu* H,const char* file_name){
int fd=open(file_name,O_RDONLY);
if(fd==-1){
perror("open");
return;
}
read(fd,H,sizeof(Inf_stu)*(H->len));
close(fd);
}
int main(int argc, const char *argv[])
{
while(1){
printf("who talk now?:[1/2]\n1:parent\n2:child\n");
int n;
scanf("%d",&n);
while(getchar()!=10);
int res=fork();
if(res==-1){
perror("fork");
return -1;
}
if(res>0){
if(n==1){
int fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd==-1){
perror("open");
return -1;
}
char str1[255];
printf("parent write: ");
scanf("%s",str1);
write(fd,str1,sizeof(str1));
perror("parent write");
close(fd);
}else if(n==2){
int fd=open(argv[2],O_RDONLY);
if(fd==-1){
perror("open2");
return -1;
}
char ch;
while(1){
char put2[255]={0};
int ace=read(fd,put2,255);
if(ace!=0){
printf("parent read:%s\n",put2);
}
}
close(fd);
}
}else if(res==0){
if(n==1){
int fd=open(argv[1],O_RDONLY);
if(fd==-1){
perror("open2");
return -1;
}
char ch;
while(1){
char put1[255]={0};
int ace=read(fd,put1,255);
if(ace!=0){
printf("child read:%s\n",put1);
}
}
close(fd);
}else if(n==2){
int fd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd==-1){
perror("open");
return -1;
}
char str2[255];
printf("child write: ");
scanf("%s",str2);
write(fd,str2,sizeof(str2));
perror("child write: ");
close(fd);
}
}
}
return 0;
}