Reading text file into a structure

This is a discussion on Reading text file into a structure within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to read a text file that is formatted as.... NetlistFile: Code: 2 3 OR 1 2 3 ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Reading text file into a structure

    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);

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is it pseudo code or real C code?
    close() is not the right function to close file pointer returned by fopen and you are missing a { }.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  2. Reading a text file into a string - fread?
    By frankchester in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 12:49 PM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  5. Reading output into a text file
    By pete212 in forum C Programming
    Replies: 8
    Last Post: 04-23-2008, 06:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21