Reading text file line by line and acting on each line...
Basically I'm trying to read a text file into a C program line by line and then act on each line.
The text file I am reading looks like this:
Each line can have one of two formats; either SET 3 1, which is setting node 3 equal to 1. Or Run 5 which means run 5 times.
SET 1 1
SET 2 1
SET 1 0
SET 3 0
SET 2 0
I am trying to read in a line, store the 3 letter word (set or run) as a string, if it is "Set" then storing the two numbers that follow, if it is "Run" then storing the one number that follows.
The stored info can then be used to do the necessary processes.
I've already had a play around with fgets, fread and fscanf but to no avail. Here is a piece of code I used earlier in the program to read in data from a slightly different text file and store it in structures:
This worked fine and was easy because of the uniformity of that file..
while(fscanf(fp, "%s %d %d %d", &LogicGates[index].GateType, &LogicGates[index].OutputNode, &LogicGates[index].InputNode1, &LogicGates[index].InputNode2) == 4)
AND 6 1 3
XOR 5 3 4
AND 7 5 2
XOR 8 6 7
where each line had four components.
Is there a way of modifying this to suit? or do I need to do something completely different?