wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [B&S] : null-terminated strings, revisited. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[B&S] : null-terminated strings, revisited." Watch "[B&S] : null-terminated strings, revisited." New topic
Author

[B&S] : null-terminated strings, revisited.

Vincent Hernandez
Ranch Hand

Joined: Oct 17, 2004
Posts: 43
Hi guys,

Now, I realize the topic of "null-terminated" has been beaten almost to death, but I didn't see anyone refer to my specific situation. If I'm mistaken on this, please point me to the proper thread.

I'm doing B&S 2.2.3, and have two lines that confuse the heck out of me.

First line is from Data File Format, Data section of the assignment:


Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information.
]

Which says "fixed field format", which would be padded by spaces.

Second quote, last paragraph in Data File Format:


All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field.


Which comes to say that strings within fields should be null-terminated if shorter than field width.

Looking with a hex editor at the data file provided by Sun, I do not see any null characters in the record fields -- only spaces.

I'm not sure what to do, other than email Sun. Currently my program applies a null character to a text value with a length shorter than the field width, and pad the rest to enforce the fixed width, such that in a field 10 bytes wide:

'f''o''o''\0'' '' '' '' '' '' '

any help would be appreciated!
[ January 04, 2005: Message edited by: Vincent Hernandez ]
Dieskun Koper
Ranch Hand

Joined: Aug 15, 2004
Posts: 85
Hi Vincent,

I thought this particular point was referred to in the following:

In general, you should not ask your question; rather you should consider the options available and make a decision about how to address the problem yourself.


I considered the following cases:
1. 'f''o''o''\0'' '' '' '' '' '' ' -> "foo"
2. 'f''o''o'''' '' '' '' '' '' ' -> "foo "

Both are valid according to my instructions. My database file used the second case (no null-termination). With relational databases it is very common to pad strings with spaces. With or without the spaces, the name of the hotel/city is "foo". So I made my application to be able to read both.

When you save a record, you have several options available:
1. Case 1
2. Case 2
3. Same as previous/other records
4...

You base your decision on your assumptions. You can assume the instructions rule and you pick 2. Or you can assume that the database file, which is probably made by a legacy tool, is right, and pick 1. Or go for another solution. As long as you defend your choice in your choices document.

Personally, I did not implement any code that wrote/updated any fields that were padded.

Regards,
Dies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [B&S] : null-terminated strings, revisited.
 
Similar Threads
Validating the Data File
[B&S] : Sun's official reply for null-terminated strings
NX:[URLyBird]Get the correct field values
how to read data from db file
.db file format problem, help please!!!