
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
pid_t pid;
char *res = "temp";
char *res1 ="temp1";
char buf[64];
char buff[32];
if (mkfifo(res, 0666) ==-1 || mkfifo(res1,0664)==-1) {
perror("mkfifo");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid > 0)
{
/* 父进程 */
FILE *fp;
FILE *fp1;
fp = fopen(res, "w");
fp1 = fopen(res,"r");
if (fp == NULL||fp1==NULL)
{
perror("fopen");
return 1;
}
fputs("world\n",fp1);
if(fp == NULL)
{
perror("error");
}
return 1;
fgets(buff,32,fp);
printf("父进程接受到:%s",buff);
fclose(fp);
fclose(fp1);
wait(NULL);
remove(res);
remove(res1);
}
else
{
/* 子进程 */
FILE *fp;
FILE *fp1;
fp = fopen(res, "r");
fp1 = fopen(res1,"w");
if (fp1 == NULL || fp==NULL)
{
perror("fopen");
return 1;
}
fgets(buf, 64, fp1);
printf("子进程接受到:%s", buf);
fputs("hello\n",fp1);
if(fp==NULL)
{
perror("fopen");
}
return 1;
pclose(fp1);
pclose(fp);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct student{
char name[20];
int age;
double math_score;
double chinese_score;
double English_score;
double physical_score;
double chemical_score;
double biological_score;
}stu;
int main(int argc,const char*argv[])
{
struct Student stu[3];
strcpy(stu[0].name,"张三");
stu[0].age = 13;
stu[0].math_score = 68;
stu[0].chinese_score = 78;
stu[0].english_score = 87;
stu[0].physical_score = 90;
stu[0].chemical_score = 90;
stu[0].biological_score = 87;
strcpy(stu[1].name,"李四");
stu[1]. age = 20;
stu[1]. math_score = 80;
stu[1].chinese_score = 80;
stu[1].english_score = 80;
stu[1].physical_score = 80;
stu[1].chemical_score = 80;
stu[1].biological_score = 80;
FILE*fp=fopen("./demo1.txt","w");
if(fp==NULL)
{
printf("fopen");
return 1;
} for(int i = 0;i < 2;i++)
{
int res = fprintf(fp,"%s %d %lf %lf %lf %lf %lf %lf\n",stu[i].name,stu[i].age,stu[i].math_score,stu[i].chinese_score,stu[i].english_score,stu[i].physical_score,stu[i].chemical_score,stu[i].biological_score);
if(res < 0){
perror("Error fprintf");
return 1;
}
}
fclose(fp);
FILE * fp2 = fopen("./demo1.txt","r");
if(fp2 == NULL){
printf("Cannot open file");
exit(1);
}
while(fscanf(fp,"%s %d %lf %lf %lf %lf %lf %lf",stu->name,&stu->age,&stu->math_score,&stu->chinese_score,&stu->english_score,&stu->physical_score,&stu->chemical_score,&stu->biological_score) != EOF){
printf("%s %d %lf %lf %lf %lf %lf %lf\n",stu->name,stu->age,stu->math_score,stu->chinese_score,stu->english_score,stu->physical_score,stu->chemical_score,stu->biological_score);
}
fclose(fp2);
return 0;
}