Meaningless Drivel is fun!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Null terminated strings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Null terminated strings?" Watch "Null terminated strings?" New topic
Author

Null terminated strings?

Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
In my Developer assignment it says that all fields is null terminated if shorter than the maximum field length.

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.


But in my database file all fields are Space filled and not null terminated? Why is the db file format different from the format that my application is expected to use?


What you thought was right today may need a refactoring tomorrow...
T. Anthony Chen
Ranch Hand

Joined: Jul 28, 2004
Posts: 38
My database is the same, containing text with trailing spaces instead of null terminated. I need to trim the strings after I read them out. When I write them back, I make them null terminated, though.

Can't explain why Sun contradicts itself. To add more confusion, maybe?


T. Anthony Chen<br />---------------<br />SCJP, SCJD, SCBCD, SCWCD, SCEA
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
There is no real contradiction, I think.
The field values are null-terminated as far as their length is less than the maximum field length.
As the space (ASCII 32) is a normal character, if field values are right-padded with it till the field length, it's normal rather normal that you don't find nulls at the end.

Example: Given a field length of 3 (the nulls are represented by 0):

"abc":no null at the end
"ab0"ne null
"ab ":no null at the end
"a00": two nulls

I personally decided to do as Anthony.

Regards,

Phil.
[ August 28, 2004: Message edited by: Philippe Maquet ]
Erkin Kanlioglu
Greenhorn

Joined: Jun 06, 2007
Posts: 20
hi people

I just want to give some info regarding this question even though it is too late to give some. (It might be helpful for future readers)

Data file which is sent by your SCJD assignment makes sense perfectly.

If fields in original db file were null_terminated, it could be too much hassle to find correct place according record number.

but with current file, we as a developer can easily jump correct place of the record.

Some clue : offset of the file is general data such as schema info or file info at the begining.

rest of info in the file are records which are fixed length. Therefore you can easily jump the record you want by using these numbers....

Note : In case of adding new record(Think there is no reusable record in db)
, add new record by obeying fixed length policy of db file. definitely it will help to find records

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The reason for the "null terminated" issue is the statement about the file being read/written by an existing legacy application. This sort of implies that the legacy application is written in C or C++ using asciz null terminated strings.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
I think the phrase we are looking for here is don't believe everthing that you read ....


SCJP<br />SCJD
Muhammad Shafique
Ranch Hand

Joined: Sep 30, 2006
Posts: 59
If the record length is fixed then field/attribute termination with null does make sense since we shouldn't add characters after the null (not even spaces). There is no concept of null after null.

I had similar text and simply ignored it!
Petr Hejl
Ranch Hand

Joined: Feb 26, 2006
Posts: 68
People, please search the forum first before posting question. This problem has been discussed MANY times and already solved... You are usually not the first one solving some issue.
Erkin Kanlioglu
Greenhorn

Joined: Jun 06, 2007
Posts: 20
Thanks people for kindly warning...
But my intention was to give some clue not answer....
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by Muhammad Shafique:
If the record length is fixed then field/attribute termination with null does make sense since we shouldn't add characters after the null (not even spaces). There is no concept of null after null.

I had similar text and simply ignored it!


This is not strictly true as it could mean padding in full with as many "0" bytes as is nescessary couldn't it? It is meerly to fill space to the end of the record after all. I do agree that that soultion would not be in the true sprit of what a null terminted string is supposed to be tho.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Erkin, may I ask how could you post into a topic in which the last post has been three years old? I am really confused, as I can only see topics that have been active in the last 30 days here...
Cless Alvein
Ranch Hand

Joined: Jun 18, 2007
Posts: 39
Sasha,
I believe that if you use the Search function, you can view older topics.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null terminated strings?