Struct and array! confusion

This is a discussion on Struct and array! confusion within the C Programming forums, part of the General Programming Boards category; Hi all, I have a small confusion on filling up struct array. I have a struct called instruction Code: struct ...

  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,047
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    read FAQ - why you should not use feof to control loop

    instead loop should look like
    Code:
    while(fscanf(f1, "%d %d,%d",...) == 3)
    {
       ...
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Code:
    while(fscanf(f1, "%d %d,%d",...) == 3)
    {
      // fill next INST with the contents read
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 07: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, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09: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


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