![]() |
| ||
| Problem with Character Arrays 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. |
| ||
| Re: Problem with Character Arrays How are you printing out the columns? If you are using any functions that assume they are null terminated and you are not terminating them then you will get strange behavior... |
| ||
| Re: Problem with Character Arrays Why not post the code that you are using to read in and display. Also make sure you are copying over and not overwriting the null terminator at the end of your char arrays. If you want to just test to see if you are, try strcat-ing a '\0' to the end of your char or manually put it in the last spot using its index value of each array. My guess is null terminator problem, see it often with new programmers. |
| ||
| Re: Problem with Character Arrays 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 Once everything is loaded, this code should recall the contents of the same element. int fn=0,al=0; 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. |
| ||
| Re: Problem with Character Arrays You can't print it out like that...cout looks for the null terminator. If you want, increase the length of each member of the struct, then put a '\0' in the last character, then print it out. |
| ||
| Re: Problem with Character Arrays 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 |
| All times are GMT -4. The time now is 11:35 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC