# [resolved] C++ Character Array problem



## lyndonp (Jul 24, 2005)

*C++ Character Array problem*

I am not an expert C/C++ programmer, and am having a bit of a problem on a school assignment. The assignment is to build a text file based database. The program has to read a schema file containing information about the individual tables (table name, filename, data field name, type, and length). Then it must be able to read the data files and do operations on the tables. 

A line from the schema file looks like this (it may wrap):

customer,customer.txt,customer_name,C,15,customer_street,C,15,customer_city,C,15:

A comma seperates the information and a colon terminates the line.

I created two structs that will hold the common data and also up to 10 sets of column data. The structs are shown below.


```
// Create a struct for the data elements
struct dataField
{
	char fieldName[15];
	char fieldType[1];
	char fieldLen[2]; 
};

// Create class for a line describing one table schema limited to 10 data elements
struct schema 
{
	char tableName[15]; 
	char fileName[15];
	int fieldsUsed;
	dataField data[10];
};
```
Without posting a large amount of code, I can get the text file read in and display the contents of the individual dataField element (such as fieldName) right after I load them. However, when I try to read the individual dataField.fieldName element any time after loading the next element, I get run together data like 

customer_name C15customer_streetC15customer_city C15

Any ideas on why it doesn't seem to give me just the data I loaded? I am using a Microsoft VC++ environment to write a command line program.

Thanks.


----------



## LoneWolf071 (Nov 10, 2004)

change 

```
// Create a struct for the data elements
struct dataField
{
	char fieldName[15];
	char fieldType[1];
	char fieldLen[2]; 
};

// Create class for a line describing one table schema limited to 10 data elements
struct schema 
{
	char tableName[15]; 
	char fileName[15];
	int fieldsUsed;
	dataField data[10];
};
```
to

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

// Create class for a line describing one table schema limited to 10 data elements
struct schema 
{
	char tableName[15]; 
	char fileName[15];
	int fieldsUsed;
	struct dataField data[10]; // this must include the struct keyword
};
```
also, please post your code here... but i'm confused as to what this will do ...


----------



## shuuhen (Sep 4, 2004)

Actually, 'struct dataField data' would be for C. A C++ compiler will create a new data type using the structure's name, so the delcaration is just like with a float or int.

C

```
struct dataField data
```
C++

```
dataField data
```
I'm not an expert with either language, but the problem is probably somewhere else in the code.


----------



## LoneWolf071 (Nov 10, 2004)

K, Right Then... Just Making Sure... Old C Programmer here(But I'm 17)... Ya... The Problem Must Lie Elsewhere in the program, please post the whole code here for more help...


----------



## ricer333 (Sep 17, 2004)

*Code Needed*

I agree, I have just been doing an awful lot of VC++ coding with file reads and writes. We all need to see more code to help you out. I believe the problem lies within how you are reading in from the streams. Different streams do different things.


----------



## lyndonp (Jul 24, 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.


----------



## LoneWolf071 (Nov 10, 2004)

J/W, Would You Have Any Problem With Posting The Whole Code, It Would Help A Whole Bunch If We Could See It All...


----------



## ricer333 (Sep 17, 2004)

*again, i concur*

Please, if you really want some help on this, give us the code, also give us a sample input / output.... this customer_name C15 stuff.... not doing it for me. Go ahead and run the code on 2,3,4, whatever number of inputs and give us what your code is outputting.


----------



## lyndonp (Jul 24, 2005)

All,

Thanks for the great help - it was the null terminator that was causing the problem. Another newbie (me) gets bit by a simple problem, but you guys are great. Thanks again.

Lyndon


----------

