我试图写组成的字符数组,一个整数值,并且一个指向管的结构写入结构的管。该结构表示单链表中的节点。阅读和用C
//Define a linked-list node object
typedef struct node{
char word[128];
int frequency;
struct node *next;
} NODE;
该程序的目标是使用管道将节点从几个并发的子进程传递给父进程。我已经实现的管道结构似乎可以正常使用字符数组,但如果我尝试传递整个节点对象,则不起作用。
//for each file argument, create a child process
for (i = 1; i < argc; i ++)
{
pipe(p[i-1]);
pid = fork();
if (pid == 0)
{
//child process
close(p[i-1][0]);
NODE *tmp;
NODE *out = freqCheck(argv[i], tmp);
write(p[i-1][1], out, sizeof(NODE));
exit(0);
}
}
if (pid > 0){
//parent process
int j;
for (j = 0; j < argc-1; j++)
{
close(p[j][1]);
NODE *tmp;
read(p[j][0], tmp, sizeof(NODE));
printf("%s\n", tmp->word);
}
}
当父进程试图从管道读取和解释结构的属性之一,我只是得到空值回来。
我使用2个元件整数数组的数组跟踪每个子进程管。上面的printf语句返回null返回时间。
任何想法我做错了什么?
代码为FrqCheck方法:
//Method for determining the most occuring word
NODE * freqCheck(char *file, NODE *max)
{
int i;
FILE *f;
char str[128];
//Set up head and tail nodes
NODE *head = NULL;
NODE *tail = NULL;
if ((f = fopen(file, "r")) == NULL)
{
//sprintf(output, "%s could not be opened.", file);
}else
{
//scan each word of the input file
while(fscanf(f, "%s ", str) != EOF)
{
//if the linked-list has no nodes, create one
if (head == NULL)
{
NODE *n;
n = (NODE *)malloc(sizeof(NODE));
strcpy(n->word, str);
n->frequency = 1;
n->next = NULL;
head = n;
tail = n;
}else{ //search the linked list for the found word.
NODE *current = head;
int found = 0;
while((current != NULL) && (found == 0))
{
//if the word is found increment the frequency
if (strcmp(current->word, str) == 0)
{
current->frequency ++;
found = 1;
}else
{
current = current->next;
}
}
//if the word is not found, create a node and add to the liked-list
if (found == 0)
{
NODE *new;
new = (NODE *)malloc(sizeof(NODE));
strcpy(new->word, str);
new->frequency = 1;
new->next = NULL;
tail->next = new;
tail = new;
}
}
}
//traverse the linked-list and find the word with the maximum frequency
NODE *tmp = head;
max = tmp;
while (tmp != NULL)
{
if (tmp->frequency > max->frequency)
{
max = tmp;
}else
{
tmp = tmp->next;
}
}
//sprintf(output, "%s %s %d", file, max->word, max->frequency);
}
//printf("%s\n", max->word);
return max;
}
是频率检查构造链表并为每个节点分配内存。该方法扫描文件中的单词,并确定哪个单词出现最多。它返回一个节点实例。 – isometrik 2011-01-27 23:35:29
但是... show source ... – Svisstack 2011-01-27 23:36:06