RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Problem with Character Arrays

Join Date: Jul 2005
Posts: 3
Reputation: lyndonp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lyndonp lyndonp is offline Offline
Newbie Poster

Re: Problem with Character Arrays

  #4  
Jul 25th, 2005
Sorry guys - I didn't want to post more code than I needed. I really figured that I must be doing something wrong with the struct declarations that cause a read of one dataField element to overrun the 15 character length. Here is the snippet of code that reads in from the CSV and writes to the struct.

Struct definations:

struct dataField
{
	char fieldName[15];
	char fieldType[1];
	char fieldLen[2]; 
};

struct schema 
{
	char tableName[15]; 
	char fileName[15];
	int fieldsUsed;
	struct dataField data[10];
};

schema schemaArray[10];


for (forvar = 0;forvar < 15; forvar++) nextWord[forvar]=' '; // clears nextWord[15] character variable
kk=0; // Resets nextWord counter
ii++; // Skip past comma (delimiter)
do 
{
	nextChar[1] = schemaLine[ii++];// Reads next character in schemaLine 
	nextWord[kk++] =  nextChar[1]; // Concats next character to the word
}
while (schemaLine[ii] != eOL && schemaLine[ii] != delim); // reads until eol or delimiter is reached
cout << nextWord << endl;// This correctly shows the expected data read in
strcpy (schemaArray[arrayLine].data[dataFieldNum].fieldName, nextWord);// loads data in schema array
cout << schemaArray[arrayLine].data[dataFieldNum].fieldName << endl; // this correctly shows data as well

Once everything is loaded, this code should recall the contents of the same element.

int fn=0,al=0;
for (al=0;al<6;al++)
{
cout << schemaArray[al].tableName << " " << schemaArray[al].fileName << " " << schemaArray[al].fieldsUsed << " " << endl; // correctly displays table name and file name data from the schema struct
	for (fn=0; fn<3;fn++)
	{
cout << schemaArray[al].data[fn].fieldName; // displays customer_name  C15customer_streetC15customer_city  C15 - should display customer_name
cout << schemaArray[al].data[fn].fieldType; // displays C15customer_streetC15customer_city  C15 - should display C
cout << schemaArray[al].data[fn].fieldLen; //displays15customer_streetC15customer_city  C15 - should display 15
	}

}

The incorrect display is shown as comments on the last cout lines. As I mentioned earlier, I think that during the retrieval of the date, my mechanism is overrunning what should be the bonds of the dataField struct, but I can't figure out why.

Thanks.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC