This program is for a homework assignment. Basically, text is read in from a file and a family tree is simulated. Here is sample input and output:
Input:
A B 3 C D X
D Y 2 M E
M F 0
C P 1 K
Output:
A-B
C-P
K
D-Y
M-F
E
X
Basically, the first two characters of each line are a "couple." Then the number represents the number of children that couple has, then the children are listed. The couple on the first line is the eldest, and couples later in the file may be children of other couples. (Clearly I have much more to do before my program accomplishes this exactly)
Regardless of what this program does, here is my issue: For each child that ends up having children, I want it to use the same for loop to create them. For example, the C
-P
couple needs to iterate through the loop ONCE to create K
. Therefore, the values of i
and numChilds
are set to 0
and 1
respectively so that it iterates once. However, those values are reset once the loop starts iterating again. Look at the two parts of the program that I labeled for debugging. The variables should be the same in both places, but they revert to their previous values somehow. Maybe I don't fully understand how forking affects variable scope, but can someone please explain it to me? Is there a way I can fix this or do I need to implement a new solution?
#include <stdio.h>
#include <sys/types.h>
int main (int argc, char* argv[] ){
pid_t pid;
FILE* inFile = fopen(argv[1], "r");
if(inFile==0){
printf( "Error opening file, terminating program\n");
return 1;
}
char person;
char partner;
int numChilds;
int inInt;
int i=0;
// boolean flag
int match;
// prime the loop by importing the first two parents
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
person = (char) inInt;
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
partner = (char) inInt;
printf("\n%c-%c\n", person, partner);
// get number of children for first pair
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
numChilds = inInt - 48;
// loop once for each new child to be created
for(i=0; i<numChilds; i++){
//////////DEBUGGING///////////////////////////////
/*
printf("%i", i);
printf("%i", numChilds);
printf("%c", '\n');
*/
//////////DEBUGGING///////////////////////////////
// get name of next child from file, set it as current person
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
person = (char) inInt;
pid = fork();
if(pid == 0){ // child process
// search for self in file to find partner
match = 0;
while(((inInt = fgetc(inFile)) != EOF) && match==0){
// if match found
if((char) inInt == person){
// set flag to stop searching file
match = 1;
// grab partner which is next name in file
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
partner = (char) inInt;
// grab number of children for that pair
inInt = fgetc(inFile);
while(inInt<33){
inInt = fgetc(inFile);
}
numChilds = inInt - 48;
printf("%i", numChilds);
// reset for loop index so it will now execute for child processes
i=0;
}
}
// if partner was never found, child will have no children, so force for loop to stop
if(match==0){
i = numChilds;
}
printf("\n%c-%c\n", person, partner);
//////////DEBUGGING///////////////////////////////
/*
printf("%i", i);
printf("%i", numChilds);
printf("%c", '\n');
*/
//////////DEBUGGING///////////////////////////////
}
else if(pid>0){ // parent process
wait(NULL);
}
}
return 0;
}