Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read from file problems

 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I have two classes in my program. Part of one class writes out dimensions of an image to a file, then adds a array after the dimensions...

OUTFILE= new DataOutputStream(new FileOutputStream(PathFileOut));
OUTFILE.writeByte(width);
OUTFILE.writeByte(height);



Now I was wondering how to get that back. I used...

IN = new DataInputStream (new FileInputStream (PATHIN));
width = IN.readByte()



But when displaying that on screen it says 96, rather then 600. Am I doing something wrong? Is it due to the width being stored as more then two bytes or something?
Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using writeByte() and readByte() instead of writeInt() and readInt()? 600 will not fit in a byte.
 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was part of my task and it had to be a writeByte and readByte. Thats the problem I am having. Is there a way of writing 600 in two bytes then and then reading 2 bytes? If I am remember one byte has 8 bits, so thats 256? so 2bytes would cover 600 but I dont know how to write it

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lora Tully:
It was part of my task and it had to be a writeByte and readByte. Thats the problem I am having. Is there a way of writing 600 in two bytes then and then reading 2 bytes? If I am remember one byte has 8 bits, so thats 256? so 2bytes would cover 600 but I dont know how to write it


I am a huge fan of schools that teach assembly language first. Students seem to develop a strong understanding and comfort level around bit-operators like &, |, !, >>, and <<... Or maybe I am bias because I spent too many years developing with assembly...


However, you don't have to get fancy. You can just divide by 100 for the high value. And get the remainder for the low value.

highvalue = x / 100;
lowvalue = x % 100;

Henry
 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I dont understand what you mean about highvalue and lowvalue?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic