السلام عليكم ورحمة الله وبركاته
ارجوكم تساعدوني في حل هالبرنامج باجر لازم اسلمه ضروري لاتبخلون علي
ا- البرنامج الأولي لازم الأب يقرأ من فايل ويطرشه حق البايب وبعدين نسوي الجايلد الي بقرأ من البايب ويطبع بس الأرقام الي عددها أقل من خمسة.
وأنا حاولت فيه بس أنه البرنت مب الي يقرأ من الفايل يعني البرنامج فأرجو أحد يساعدني:
وهاي الكود
#include<stdio.h
int main()
{
int countword=0,p[2],countpipe=0,i;
char word[20],word1[20],c;
FILE * txtfile;
txtfile=fopen("textfile.txt","r");
pipe(p);
while(fscanf(txtfile,"%s",word)!=EOF)
{
write(p[1],word,20);
countpipe++;
}
fclose(txtfile);
if(!(fork()))
{
for( i=0;i<countpipe;i++)
{
read(p[0],word1,20);
if(strlen(word1) <5 )
{
printf("\nThe words is :%s ",word1);
countword++;
}
}
printf("\nThe total number of words :%d \n",countword);
exit(1);
}
else
wait(NULL);
return 0;
}
2- البرنامج الثاني نفس الشيء بس البرنت يفتح اي اسم نحن نعطيه للفايل وبعدين يكتبه بالبايب ويطرشة للجايلد الي بعد الحروف ويطبع مجموعها ويطبع مجموع الكلمات بعد. ولو كمل اليوزر ماعنده فايل ثاني يطلع من البرنامج (e)
وهاي الكود حاولت فيه :
#include<stdio.h>
#include<ctype.h>
void main()
{
char filename,word;
FILE *fp;
int p[2],r,countword=0;
printf("Enter a name of a file\n");
scanf("%s",filename);
do
{
fp=fopen(filename,"r");
pipe(p);
r=fork();
if(r==1) //parent will send the content of the file to the pipe
while(fscanf(fp,"%s",word)!=EOF)
{
write(p[1],word,20);
countpipe++;
}
fclose(fp);
wait();
printf("\n Enter another file name or press(E) to exit\n");
//if(!(fork()))
if(r==0) //child will read the contents of the pipe and count the number of words & chars.
{
for( i=0;i<countpipe;i++)
{
read(p[0],word1,20);
//calculating the number of the words in the pipe
countword++;
//calculating number of characters in the pipe
countchar+=strlen(word1)+1;
}
printf("\nThe number of words=%d",countword);
exit(1);
}
//else
// wait(NULL);
// printf("\n Enter another file name or press(E) to exit\n");
}
while(strcmp(filename,"e")|| strcmp(filename,"E"));
}