Array of Structures

This is a discussion on Array of Structures within the C++ Programming forums, part of the General Programming Boards category; hey all, Just wondering if someone might know what is going wrong here. I have a file that looks like ...

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    Array of Structures

    hey all, Just wondering if someone might know what is going wrong here.
    I have a file that looks like this ...

    Code:
    ...
    V              2            103       362731.9      5606201.4          896.2 
    V              2            104       362722.8      5606184.1          895.8 
    V           2875            101       361697.7      5607396.2            902 
    V           2875            102         361687        5607386          902.2 
    ...
    ... but with tons of lines in it. And im tring to load it up into an
    array of structures. However when i try to display the data back to make
    sure its all there. I only get the last line of data, then a bunch of garbage.
    Heres the code im using ...

    Code:
    
    struct coordinate_data
    {
    	char   byte;
    	int    lineNumber, binNumber;
    	double xcoord, ycoord;
    	float  elevation;
    };
    typedef struct coordinate_data Data;
    
    void GenerateCoordFile(char fileName[])
    {
    	Data *Records;
    
    	short Size = 0;
    
    	Records = NULL;
    
    	fstream inFile(fileName, ios::in);
    	if(!inFile)
    	{
    		cerr << endl << "Input file " << fileName << " could not be opened!" << endl;
    		abnormalCompletion();
    		exit(1);
    	}
    
    	do
    	{
    		if(!NewElement(Records, Size))
    		{
    			cerr << endl << "Insufficent memory to store additional records." << endl;
    			abnormalCompletion();
    			exit(1);
    		}
    
    	}while(inFile >> Records->byte >> Records->lineNumber >> Records->binNumber
    				  >> Records->xcoord >> Records->ycoord >> Records->elevation);
    
    	DisplayData(Records, Size);
    
    	delete [] Records;
    }
    
    short NewElement(Data *& Records, short & Size)
    {
    	Data *temp;
    
    	temp = new Data[Size+1];
    
    	if(temp == NULL)
    	{
    		return 1;
    	}
    	
    	memcpy(temp, Records, (sizeof(Data) * Size++));
    
    	delete [] Records;
    
    	Records = temp;
    
    	return 1;
    }
    
    void DisplayData(Data *& Records, short Size)
    {
    	Data *END = Records + Size,
    		 *search;
    
    	search = Records;
    
    	while(search < END)
    	{
    		cout << fixed << setprecision(1)
    		     << search->byte << "\t" << search->lineNumber << "\t" << search->binNumber << "\t"
    			 << search->xcoord << "\t" << search->ycoord << "\t" << search->elevation << "\r";
    		
    		search++;
    	}
    }

    Any help would be great ... thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> inFile >> Records->byte

    Records is an array, but you're using it like a plain pointer. Did you want Records[size-1].byte instead?
    Last edited by Daved; 09-21-2007 at 09:47 AM.

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    well when i add a cout to the do while loop, it seems to be reading it in correctly. Its just when i display it back. It only has the last line, then garbage.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    That's because when you use an array like a pointer, you are modifying the first element in the array. So because of the problem I mentioned, your code modifies the first element in the array over and over, which overwrites the previous line of data each time. When it is done, only the last line of data is stored, and it is stored in the first slot in the array. The rest of the array is left untouched and is therefore filled with garbage.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are always reading into Records->xxx - Records always points to the FIRST element.

    You want to read into.
    Records[size-1].xxx, as that is the LAST element in the list.

    Edit: Daved beat me to it. Got to learn to type quicker (not to mention that Daved already said this was the problem EARLIER).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Thank you very much guys. All is good now, i really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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