The ultimate goal is creating an array of strings from a file. So I have a file that I need to get temporary variables from temp1, temp2, temp3, etc. I wont know how many are in advance. My first thought was to create a normal string from the variables I do find using strcat then splitting that using tokens. I thought I could bypass this by just adding the string normally.
while(feof(inputFile))
{
fscanf(inputFile, " %s ", readIn);
if(strcmp("AND", readIn) == 0 || strcmp("OR", readIn) == 0 || strcmp("NAND", readIn) == 0 || strcmp("NOR", readIn) == 0 || strcmp("XOR", readIn) == 0)
{
fscanf(inputFile, "%s ", readIn);
fscanf(inputFile, "%s ", readIn);
fscanf(inputFile, "%s ", tempVar);
//printf("the char is %c", tempVar[0]);
//check to see if its a temp variable since temps are always lower case
if(tempVar[0] == 't')
{
numVars++;
tempHeaders = realloc(tempHeaders, sizeof(char*) * numVars);
int temp = numVars -1;
printf("the variable is %s and the number is %d\n", tempVar, temp);
tempHeaders[numVars - 1] = tempVar;
}
numGates++;
}
}
At this point the number of variables are right, the issue is when adding to the array tempHeaders. the entire value of the array will be whatever the last word of the file is, i.e. if the lastword is out. tempHeaders i - n would all be out. Any ideas? Thank you
I've edited out feof from the while loop and changed to "while(fscanf(inputFile, "%s ", readIn) != EOF){"
sample input would be
OR IN1 IN2 temp1
OR IN3 IN4 temp2
OR IN5 IN6 temp3
OR IN7 IN8 temp4
AND temp1 temp2 temp5
AND temp3 temp4 temp6
XOR temp2 temp6 OUT1
sample output at this time
the variable is temp1 and the number is 0
the variable is temp2 and the number is 1
the variable is temp3 and the number is 2
the variable is temp4 and the number is 3
the variable is temp5 and the number is 4
the variable is temp6 and the number is 5
Printing OUT1
Printing OUT1
Printing OUT1
Printing OUT1
Printing OUT1
Printing OUT1
numGates = 7, numVars = 6
the prints should be temp1, temp2, temp3, etc...