我有一个程序,我需要读取几个管道的内容,比较单词,打印最小的单词,从该管道(并且仅该管道)中获取一个新单词,再次比较,直到所有管道都为空。
我有几个问题:
1:由于某种原因,我只能从一个管道读取。当试图从其他人那里阅读时,它一直没有给我任何东西,即使我将管道设置为其他任何人,它也只适用于该管道。
基本上,当我想要它时,我无法让 sortOutput 切换到其他管道。
2:由于某种原因,我似乎无法检测到数组元素何时为空,因此它将“”与单词进行比较,而“”总是较低。
据我所知,发生的事情是 sortOutput 设置为最后一个管道,然后它继续从该管道而不是其他管道读取,或者如果通过循环从其他管道强制读取,则什么也没有。我不确定为什么,但是如果我将 sortOutput 显式设置为不同的管道(没有循环,全局声明),它会从任何其他管道中读取所有单词就好了。我认为,它正在将 sortOutput 切换到导致问题的循环内的其他管道。奇怪的是,在设置初始字数组时,将 sortOutput 切换到其他管道就可以了。作为说明,我必须使用 fgets,我没有选择。
这是抑制器的代码,我已经评论了我认为问题正在发生的地方:
//Suppressor - Reads one word from each pipe, compares, prints largest. Gets next word from that one pipe, compares, prints largest.
//Suppressor deletes duplicate words
//Reads from pipefds_two[count*2] position
char* words[numChildren];
int index, cont=1;
char* smallest;
int smallestIndex;
int checker;
int duplicateCount = 0;
int kindex;
char* temptwo;
int length;
int nullCount = 0;
int counter = 0;
FILE* sortOutput;
for(kindex = 0; kindex < numChildren; kindex++){ //Initializes array with beginning values
sortOutput = fdopen(pipefds_two[kindex*2], "r");
fgets(buffer, PIPE_BUF, sortOutput);
words[kindex] = strdup(buffer);
//fflush(sortOutput);
close(pipefds_two[(kindex*2)+1]);
}
while(counter < 13){ //This is where it prints out lowest values each "round", gets new words, and gets rid of duplicates
printf("\nCurrent words in array (0, 1, 2): %s %s %s", words[0], words[1], words[2]);
for(index = 0; index < numChildren; index++){
if(words[index] != NULL){ //Searches for first value in array that's not null to be "lowest" value
smallest = words[index];
smallestIndex = index;
printf("Found first non-null word: %s", smallest);
break;
}
}
printf("Suppressor WHILE \n");
nullCount = 0;
printf("smallest word assigned: %s ", smallest);
printf("smallest index %d\n", smallestIndex);
for(index = 0; index < numChildren; index++){ //need to loop through each pipe and pull a word, THEN compare them all!
printf("Suppressor FOR (index: %d word:%s)", index, words[index]);
if(words[index] == NULL){ //Fills in a NULL gap in the array with a new word from the corresponding pipe
bzero(buffer, PIPE_BUF);
sortOutput = fdopen(pipefds_two[index*2], "r"); //THIS IS PROBLEM! Here, or around here!
fgets(buffer, PIPE_BUF, sortOutput);
words[index] = strdup(buffer);
//fflush(sortOutput);
printf("the word which replaces a NULL: %s", buffer);
}
}
for(index = 0; index < numChildren; index++){ //COMPARE ALL VALUES NOW THAT IT IS POPULATED
printf("compare FOR loop index: %d\n", index);
if((index != numChildren) && (words[index] != NULL) && (index != smallestIndex)){
printf("IF statement, (current arrayWord: %s)(smallest: %s)", words[index], smallest);
checker = strcmp(smallest, words[index]);
//printf("checker\n");
if(checker > 0){
smallest = words[index];
smallestIndex = index;
printf("New smallest assigned: %s New Smallest Index: %d\n", smallest, smallestIndex);
}else if(checker == 0){
printf("Same word\n");
words[index] = NULL;
duplicateCount++;
}else{
printf("ArrayWord is larger, smallest staying the same\n");
}
} if(index == numChildren-1){ //reached the end of the list
printf("The smallest this round is: %s", smallest);
words[smallestIndex] = NULL;
}
}
for(index = 0; index < numChildren; index++){ //Check for removed words!
printf("Checking if entries are null in array: index %d\n", index);
if(words[index] == NULL){
nullCount++;
printf("words at index null num: %d\n", nullCount);
}
}
//check to see if everything is null
counter++;
}
我知道 while 循环只设置为 13,但是当我运行它并可以看到发生了什么时,它不是无限循环。
提前谢谢大家!