This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read and Write doubts

 
Michel Bertrand
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !
I'm in doubt about my database reading and writing methods. When I
write any record in the database, if the field length is lower
then its value some "00" fills its size. I could see it checking
the database file using a binary editor. So, at screen, I have
some little squares when showing this record. See the part of the
writing method below:

Where: buffer has the content.
fields is an array of objects encapsulating the database fields
offset control the data structure
CHARSET is US-ASCII
Checking the default database that was send me there's "20" instead of
"00" in the records probably representing the empty part.
Reading my assignment, I found an instruction to write only the content
length for any field. So, probably I cannot complete the field size
using blank spaces "20".
In this context, how could I avoid the little squares ? Did I make a
mistake ?
Regards,
Michel.
[Andrew: put the source code between [code] and [/code] UBB tags]
[ March 16, 2004: Message edited by: Andrew Monkhouse ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michel,
I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
As for your question ....
If you want to get rid of those squares, you could try doing a trim() on the String - this was a common solution to this problem in the Fly By Night Services assignment where strings were null terminated.
In the newer assignments, the instructions state that the fields are null terminated, but in reality they seem to be padded with spaces. This is apparently a deliberate decision by Sun.
I think most people here are padding the data with spaces when they write it back to disk.

Reading my assignment, I found an instruction to write only the content
length for any field.

Can you please tell us what that instruction is?
Regards, Andrew
 
Michel Bertrand
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Thank you for your reply and your code hints !
I found in my assignment:

But it was not what I saw in the database file. I encontered those padded "20" (blank space characters) instead of null terminated. That's it.
What do you think ? I padded the blanks too, but I'm a little afraid of it. I tried the "trim()", but it did not work for me and I did not find the reason yet.
Thanks again !
Michel.
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michel
Originally posted by Michel Bertrand:


But it was not what I saw in the database file. I encontered those padded "20" (blank space characters) instead of null terminated. That's it.

Yes, that's right. For some they are padded with blank spaces only. But we have to take into consideration that there might be a chance for terminating with null.

What do you think ? I padded the blanks too, but I'm a little afraid of it. I tried the "trim()", but it did not work for me and I did not find the reason yet.

"trim()" should work Michel. I think am using it and worked for me.

Thanks again !
Michel.

Good Luck.
 
George Marinkovich
Ranch Hand
Posts: 619
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michel,
The assignment instructions state that fields may be null-terminated (if they are less than the maximum length), so you should be able to read fields that are null-terminated (even though there may not be any example of that in the Sun supplied database file). You also need to be able to handle fields that are not null-terminated, that is, ones that use the maximum length of the field. One way for this to happen is for the field to be right padded with blank spaces. In this case you probably want to do a trim() to get rid of the useless blank space.
When writing the field it's probably easier to just null-terminate the fields if they are less than the maximum length. But the choice is yours, you could also right pad the fields with blank spaces. Either way is consistent with the assignment instructions requirement. Null-terminating is probably more efficient.
 
Michel Bertrand
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys !
Thanks for all !
Regards,
Michel.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic