Thread: Struct and array! confusion

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Struct and array! confusion

    Hi all, I have a small confusion on filling up struct array.

    I have a struct called instruction
    Code:
    struct instruction
    {
    int address;
    int source;
    int destination;
    };
    
    instruction INST[100];
    while(!feof(f1)
    {
    fscanf(f1,"%d %d,%d\n",&address,&source,&destination);
    //ADD the contents to INST here.
    
    }
    I read a file with many lines of format( address, source,destination)
    and everytime i read a line, I want to add it to INST[]..

    Could someone please help me to add the contents to this?
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How about fscanf()'ing each line read directly into an element of INST[] instead of doin' it in 2 steps.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Hi Thanks for your reply.

    I am doing some modification to the contents read to the file and only after that I am pushing it to INST[](of type instruction). That is where I am stuck

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    read FAQ - why you should not use feof to control loop

    instead loop should look like
    Code:
    while(fscanf(f1, "%d %d,%d",...) == 3)
    {
       ...
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Ok. but could anyone help me with filling up INST[] with the contents read?

    if( some condition is met)
    {
    then I have to add the file line contents read in this loop to INST[]
    }
    else
    {
    add 0 to the INST[]
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    while(fscanf(f1, "%d %d,%d",...) == 3)
    {
      // fill next INST with the contents read
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

Tags for this Thread