Thread: Storing array of structures in C

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    26

    Storing array of structures in C

    So i have a program with the structure
    Code:
    char  record[20];
    typedef struct{
    int id;
    char title[20];
    char subject[20];
    char faculty [20];
    int level;
    char fname[25];
    }report;
    int recno,i;
    report list[100];
    I need to store the hundred records into a file.I searched a lot in the internet and my notes but i got confused.Please tell me about the functions and elements i should use/introduce. thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well, if you had a single report, you would have

    Code:
    report student;
    fscanf("%d",&student.id);
    For an array, this becomes
    Code:
    report students[100];
    for ( i = 0 ; i < 100 ; i++ ) {
      fscanf("%d",&students[i].id);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    26
    thnks
    Its often the simple things we wonder about..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-11-2011, 02:37 PM
  2. 2D array storing
    By anirban in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 07:16 PM
  3. Storing an array of structures?
    By Sparrowhawk in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 03:07 AM
  4. Storing in an Array
    By peckitt99 in forum C++ Programming
    Replies: 24
    Last Post: 10-10-2006, 01:01 AM
  5. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM