Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Char/Byte Streams  RSS feed

 
Andrew Stallard
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this text file- xanadu.txt

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

I can use this Char Stream program to copy it to thatone.txt:



Now, if I want the variable x or the life.read() value printed, something goes wrong. I get this output in thatone.txt

I aauddKbaKa
ttl lauedm ere
hr lh h ardrvr a
hog aen esrls omnDw oasnessa

What I am wondering is why printing the values for x and life.read() seem to change them?

In addition, if I don't use what appears to be the redundant variable x on lines 9-10 I have this code:



That yields this result:



nXnd i ul hnAsaeypesr-oedce:WeeAp,tesce ie,rnTruhcvrsmaueest a
ont uls e.

I am just curious why these things should be important.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read() returns the next character/byte and then skips to the next one. That's why several characters are missing when you call read() more than once in the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!