As the title says I'm tryng to create a program in C in which 2 son process research a vowel(son2) and a consonant(son2). To perform the research i use 2 function, the first to research a vowel and the second to research a consonant. I give to the program 3 more file from command line:
-1° is the file in which is stored a combination of vowel and consonant
-2° is the file in which will be stored all the vowel founded
-3° is the file in which will be stored all the consonant founded
The program is compiled without error/warning but it doesn't complete the research correctly, here is an example:
-first file: qwerty
-second file: ey
-third file: qr
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
int test_vowel(char ch)
{
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'y')
return(1);
else
return(0);
}
int main(int argc, char *argv[])
{
char c, d;
int s;
pid_t pid1 = 10, pid2 = 10;
if((pid1 = fork()) < 0)
{
printf("Error in the creation of the first fork\n");
_exit(0);
}
if(pid1 > 0)
{
if((pid2 = fork()) < 0)
{
printf("Error in the creation of the second fork\n");
_exit(0);
}
}
int input = open(argv[1],O_RDONLY);
int output1 = open(argv[2],O_RDWR | O_CREAT | O_TRUNC, 0666);
int output2 = open(argv[3],O_RDWR | O_CREAT | O_TRUNC, 0666);
if(pid2 == 0)
{
while((s = read(input, &c, 1)) != 0)
{
if(test_vowel(tolower(c)) == 1)
{
printf("I've read a vowel = %d\n", d);
write(output1, &c, 1);
}
}
}
if(pid1 == 0)
{
while((s = read(input, &d, 1)) != 0)
{
if(test_vowel(tolower(d)) == 0)
{
printf("I've read a consonant = %d\n", d);
write(output2, &d,1);
}
}
}
}
I use these command to compile it
gcc c.c
./a.out c.txt v.txt b.txt
Thanks in advance for the help!
EDIT(final) Code (now is working) simplified following all of the tips of David C. Rankin