Thread: Structures and Arrays

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    36

    Structures and Arrays

    Hey,

    I've read the tutorials on structs and arrays, and my program needs to be much like this piece below...

    Code:
    struct Person {
      char *name;
      int age;
      float height;
    } people[] = {
      {"Prelude", 25, 5.9f},
      {"Generic person", 20, 6.0f}
    };
    ...except my program scans in the name/age/height from a file instead of typing it in. How can I scan this in?

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, I'd make name and people into arrays (unless you also want to deal with dynamic allocation). Then I'd take a look at the file format, but likely use some form of fgets/sscanf to read the data.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Write a function that reads the name/age/height into a structure pointed to by one of its paramaters. You'll have to come up with some way to store the name, as the struct you list uses a pointer to char, and each copy of the struct will have its own name. You can do something like:
    Code:
    /* assume the local variables:
     *   int newAge;  // contains the age read from the file
     *   float newHeight; // contains the height read from the file
     *   char newName[MAXLEN]; // contains the name read from the file
     *   struct Person *newPerson; // contains a pointer to the array element to be filled in
     */
    char *nBuff = malloc(strlen(newName)+1);
    if (nBuff == NULL)
    {
        /* Can't allocate a name, abort or handle the error some other way */
    }
    else
    {
        strcpy(nBuff, newName);
        newPerson->name = nBuff;
        newPerson->age = newAge;
        newPerson->height = newHeight;
    }
    Call the function with a pointer to each successive element until you reach the end of your file. When you're done, you should remember to call free() on the name member of each array entry you read in, otherwise you'll have a memory leak.

    Note: I know that this can be coded in fewer steps/more efficiently, but I'm trying to be as clear as possible here.
    Insert obnoxious but pithy remark here

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    So is there no way to do something like this:

    Code:
    struct Person {
      char *name;
      int age;
      float height;
    } people[] = {
      {"fscanf(inp, "%s", &people[1])", fscanf(inp, "%s", &people[2]), fscanf(inp, "%s", &people[3])},
    };

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are ways, but certainly not some nice, easy one-liner.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 12:31 PM
  2. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  3. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:48 PM
  4. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM