• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to a File

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I am taking an input from a file whose contents are 1,4,2,5,6,4,7 etc.. and am sorting it. Everything works fine but I am unable to write the output back to another file. Basically.. I am unable to write anything back to a file. File Numbers2.txt is getting created tho.

Am pasting the code below. Am not gettin any compilation error.

i

Can anybody tell me where am doing wrong

Thanks.
[ July 01, 2008: Message edited by: Arjun Reddy ]
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you need to flush the output and close the file. After your loop code block containing this line:



add



it wouldn't be bad to close() reader either.
[ July 01, 2008: Message edited by: Mark Freeman ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark it works now but I am gettin some unnecessary characters in my ouput file. The output am gettin is 2 4556677890


I changed the code to



Can you tell me why?

Thanks.
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may not make a difference (I am not where I can test it), however, have you tried flush() outside of the loop? If I understand the purpose of flush() correctly, it flushes the buffer. I would want to do this just once, after I am finished writing everthing out. It seems worth a try.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Thanks for ya reply. Um.. I tried doing that but am still getting the illegal characters between the numbers in my output. It's nothing imp but I was just wondering what's causing this.

Thanks.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I figured out what was going wrong. I had after this statement writer.write(arr[i]), this one writer.write(i); I put it there to test and did not comment it later on. Now, I've commented it and the code's working fine and am able to see the correct output. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic