Hi,
I'm trying to read a text file that is formatted as....
NetlistFile:
Code:
2 3
OR 1 2 3
AND 4 6 4
AND 6 4 3
Where the two number on the first line need to be read into two ints called nGates and nNodes respectively.
And then each following line needs to be read into a new structure in the form below:
Code:
// Setup Gate Structure
typedef struct
{
char GateType[4];
int OutputNode;
int InputNode1;
int InputNode2;
} LogicGate;
It prints to the command window fine and I get a list exactly matching the text file. But when I try to recall information later - for example LogicGates[0].GateType - The program only ever gives the very final gate type, in this example 'AND' and for any other index number ie LogicGates[1].GateType - gives nothing.
Can you tell me what I'm doing wrong??
Thanks,
Code:
// Read in NetlistFile
FILE *fp= fopen(NetlistFile,"r");
LogicGate *LogicGates = malloc(200 * sizeof *LogicGates);
int index = 0;
// Get nGates and nNodes
while(fscanf(fp, "%d %d", &nGates, &nNodes) == 2)
// Print to screen
printf("\n %d %d", nGates, nNodes);
// Get Gate data and structure
while(fscanf(fp, "%s %d %d %d", &LogicGates[index].GateType, &LogicGates[index].OutputNode, &LogicGates[index].InputNode1, &LogicGates[index].InputNode2) == 4)
// Print to screen
printf("\n %s %d %d %d", LogicGates[index].GateType, LogicGates[index].OutputNode, LogicGates[index].InputNode1, LogicGates[index].InputNode2);
index++;
close(fp);