제목에 두 아들이 모음 (son2)과 자음 (son2)을 연구하는 C 프로그램을 작성하려고합니다. 연구를 수행하기 위해 나는 2 개의 함수를 사용하는데, 첫 번째는 모음을 연구하고 두 번째는 자음을 연구합니다. 내가 커맨드 라인에서 프로그램 3 개의 파일에 부여 :C fork()와의 모음 및 자음 연구
31 °가
29 ° 자음, 모음의 조합에 저장되는 파일은 파일이있는 모든 저장 될 것이다 모음은
-3 °
파일이있는 저장됩니다 설립 된 모든 자음이 프로그램은 오류/경고없이 컴파일하지만, 제대로 연구를 완료하지 않습니다, 여기에 예입니다
설립 :- 첫 파일 : 쿼티
- 두번째 파일 : 어이
- 제 3 파일 : QR 나는 사전에 그것을
gcc c.c
./a.out c.txt v.txt b.txt
감사를 컴파일하려면 다음 명령을 사용하여
#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);
}
}
}
}
도움!
EDIT (최종) 코드 (지금 노력하고 있습니다) 당신은 어떤 특정 값에 pid1
또는 pid2
를 초기화하지 않는 데이비드 C. 순위
'tolower (ch)'를 사용하면'if '조건의 수를 2 배로 줄일 수 있습니다. 왜 1 대신 2 개의 함수가 사용됩니까? 그냥'if (isalpha (ch)) {ch = tolower (ch); if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u') return 1; 그렇지 않으면 2를 반환합니다. }'(또는 자음 또는 모음을 나타내려고하는 것) –
제 책은이 기능을 인용하지 않았다고 말해야합니다. 팁을 주셔서 감사합니다! – Allen