This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K*B 1.5 book . PG 433 Error

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a code in K&B that shows how to use FileWriter and FileReader to write and read the contents of a file.


The output :
abc
def
Boxes till the end....... [Square boxes]

As the read(char []dest) method reads the number of characters that is equal to the size of the declared array dest.
In the above program since of size of array is far greater than the length of the file , Using for(char c est) { } will iterate to the length of the array instead of the lenght of the file. Hence the above code should be modified to iterate the dest[] till the size returned by the read(). Hence the code should be modified as follows.


This will now display the excat contents of the file.
This is not mentioned in the K&B errata in the site.
Hope am correct.
Thanks
Deepak
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent catch. If I'm not mistaken, most systems will render the remaining zero-value bytes in the array as spaces, so there probably will not be a visible difference to the casual observer. But your correction is absolutely right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic