array of structures, data from file

This is a discussion on array of structures, data from file within the C Programming forums, part of the General Programming Boards category; Making one structure....Ok Making an array of structures....I'm stuck DataFile: Code: Sony 29" TV 699.99 5 N/A Electronic Flat Screen ...

  1. #1
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

    array of structures, data from file

    Making one structure....Ok
    Making an array of structures....I'm stuck

    DataFile:


    Code:
    Sony 29" TV
    699.99
    5
    N/A
    Electronic
    Flat Screen WEGA
    
    Sony DVD Player
    400.5
    11
    N/A
    Electronic
    Proressive Scan
    Load data to structure:

    Code:
    void loadData(structure *inventory,FILE *dataFile)
    {
         fgets(inventory->name, MAXchar,dataFile);
         fgets(inventory->price, MAXchar,dataFile);
         fgets(inventory->noofItems, MAXchar,dataFile);
         fgets(inventory->expiry, MAXchar,dataFile);
         fgets(inventory->type, MAXchar,dataFile);
         fgets(inventory->detail, MAXchar,dataFile);
    }

    Print to Screen:
    Code:
        puts(inventory.name);
        puts(inventory.price);
        puts(inventory.noofItems);
        puts(inventory.expiry);
        puts(inventory.type);
        puts(inventory.detail);
    Now how do i convert this so i can take in the two sets of data..?

    Any help would be appreciated.

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    My attempt so far...
    Code:
    #define dataFileName "WMinventory.dat"
    #define MAXchar (100)
    
    typedef struct{
          char name [50];
          char price[50];
          char noofItems[50];
          char expiry[50];
          char type[50];
          char detail[50];
    }structure;
    
    void loadData(structure *[],FILE *);
    FILE *open_file(char [], char []);
    
    int main(void)
    {
        structure inventory[2];
        FILE *dataFile;
        
        dataFile = open_file(dataFileName ,"r");
        
        loadData(inventory,dataFile);
        
        puts(inventory[1].name);
        puts(inventory[1].price);
        puts(inventory[1].noofItems);
        puts(inventory[1].expiry);
        puts(inventory[1].type);
        puts(inventory[1].detail);
        
        fclose(dataFile);
        getchar();
        return 0;
    }
    
    void loadData(structure *inventory[],FILE *dataFile)
    {
         fgets(inventory[1]->name, MAXchar,dataFile);
         fgets(inventory[1]->price, MAXchar,dataFile);
         fgets(inventory[1]->noofItems, MAXchar,dataFile);
         fgets(inventory[1]->expiry, MAXchar,dataFile);
         fgets(inventory[1]->type, MAXchar,dataFile);
         fgets(inventory[1]->detail, MAXchar,dataFile);
    }
    
    FILE *open_file(char fileName[], char accessType[])
    {
        FILE *fptr;
        if ( NULL == (fptr = fopen(fileName, accessType))) {
            puts("FILE OPEN ERROR:");
            puts(fileName);
            puts("\n");
            system ("pause");
            abort();
            }
        return fptr;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Arrays are always passed as a pointer, there is no need for you to do anything special
    Code:
    void loadData(structure [],FILE *);
    FILE *open_file(char [], char []);
    
    int main(void)
    {
        structure inventory[2];
        FILE *dataFile;
        
        dataFile = open_file(dataFileName ,"r");
        
        loadData(inventory,dataFile);
        
        puts(inventory[1].name);
        puts(inventory[1].price);
        puts(inventory[1].noofItems);
        puts(inventory[1].expiry);
        puts(inventory[1].type);
        puts(inventory[1].detail);
        
        fclose(dataFile);
        getchar();
        return 0;
    }
    
    void loadData(structure inventory[],FILE *dataFile)
    {
         fgets(inventory[1].name, MAXchar,dataFile);
         fgets(inventory[1].price, MAXchar,dataFile);
         fgets(inventory[1].noofItems, MAXchar,dataFile);
         fgets(inventory[1].expiry, MAXchar,dataFile);
         fgets(inventory[1].type, MAXchar,dataFile);
         fgets(inventory[1].detail, MAXchar,dataFile);
    }
    Note that [0] is the first element of the array, not [1]
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    FINALLY.....evrything works.....thx a lot salem

    EDIT:
    prob no.3

    right now i am using all the members of my structures as strings...but i want price and noofItems to be float and int repectively...

    I dont want to end up messing around with fscanf's and fgets. Is there a way around???

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Use fgets() to read the strings which aren't strings into a temporary buffer, then use sscanf() to extract the information you want

    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, fp );
    sscanf( buff, "%f", &inventory[1].price );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Now why doesnt this work....please dont comment on the feof..


    [EDIT:]My bad.....

    int price[50]
    ^^^^made me think....
    Last edited by nomi; 01-11-2004 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Structures
    By dmkanz07 in forum C Programming
    Replies: 12
    Last Post: 04-19-2007, 09:55 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. extracting data from file putting it into an array..
    By Cat00 in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2005, 01:48 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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