• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Magic cookie value

 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

What is the purpose of the 4 bytes magic cookie value? It seems pretty useless in the project. It is not even mentioned in any other places of the instructions.html file other than the data section. If there is no purpose for it, should I store the value? Cause if I were to use RandomAccessFile, I can choose to leave the value alone.

Thanks for anyone who will answer my questions.
 
Frank Bush
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that the magic cookie identifies the database file format.

When reading what is supposed to be a database file, you should verify that the first 4 bytes match the magic cookie value. If they do not, you can throw an exception. Chances are that you are reading Resume.doc or MyLastVacationInTheCaribbean.mpeg

Back to your question, I do not see any reason why you would want to store this value.
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a fixed value for this magic cookie value? I mean everyone have the same value is it? If not where do I get to find this value other than by reading the flat file in the first place? Cause I need to read the first four bytes than do a comparison test using == when the program starts right?
 
Frank Bush
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an integer, mine is 514 (decimal), but yours might be different. You can find out by printing the return value of where dbFile is a freshly opened RandomAccessFile .
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Clivant,

Is there a fixed value for this magic cookie value? I mean everyone have the same value is it?


No - there are lots of different values out there.

Anyone who has a Fly By Night Services assignment is almost guaranteed to have a different magic number than someone who has a Bodgitt & Scarper assignment and they are almost guaranteed to have a different magic number than someone who has an URLyBird assignment.

Furthermore someone with version 1.1.1 of URLyBird may have a different magic number than someone with version 1.1.2 which may be different from version 1.2.1 and so on.

The reason for this is that the file formats are sometimes different. So if the magic numbers don't match, chances are that the program you wrote won't be able to meaningfully read the file.

If not where do I get to find this value other than by reading the flat file in the first place? Cause I need to read the first four bytes than do a comparison test using == when the program starts right?


You do have to read the file at least once to get the magic number. Thereafter you can store the magic number and validate that it matches in future readings.

Regards, Andrew
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew and Frank

I got it now. Enlightened by both your guidance.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the magic cookie value is assignment specific, so specific that even the grader's test db file will be checked against with this value for further evaluation. Am I right?
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andy,

I think so also.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic