gpt4 book ai didi

C 研究元音和辅音 fork()

转载 作者:行者123 更新时间:2023-11-30 20:59:23 25 4
gpt4 key购买 nike

正如标题所说,我正在尝试用 C 创建一个程序,其中两个儿子处理元音(son2)和辅音(son2)。为了进行研究,我使用了两个函数,第一个用于研究元音,第二个用于研究辅音。我从命令行向程序提供了另外 3 个文件:

-1°是存储元音和辅音组合的文件

-2°是存储所有元音的文件

-3°是存储所有辅音的文件

程序编译时没有错误/警告,但没有正确完成研究,这是一个示例:

-第一个文件:qwerty

-第二个文件:ey

-第三个文件: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);
}
}
}

}

我使用这些命令来编译它

gcc c.c
./a.out c.txt v.txt b.txt

预先感谢您的帮助!

编辑(最终) 按照 David C. Rankin 的所有提示简化了代码(现在正在运行)

最佳答案

您无需将 pid1pid2 初始化为任何特定值。如果 pid1 为零,则根本不需要将 pid2 设置为任何值。但是您的第一个 if 检查 pid2 的值。这不可能是正确的。

关于C 研究元音和辅音 fork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com