Posts

Showing posts with the label io

How to properly using EOF?

Image
How to properly using EOF? I have question about EOF. First of all, I am coding a simple program that is coping/printing the user's input. However, the program copies the EOF also in the output. For an example, my O.S is Window and my EOF works when I type (Enter -> cntrl + z -> Enter) in order. If I input "Hello" + Enter + EOF key combination, the output prints the weird letter('?') at the end of the copied user input. How can I get rid of the '?' at the end of the output, and why is it happening? #include <stdio.h> void copy(char to, char from); main() { int i; int c; char origin[10]; char copied[10]; for(i = 0; (c = getchar()) != EOF; ++i) { origin[i] = c; } copy(copied, origin); for(i = 0; i < 10; i++) putchar(copied[i]); } void copy(char to, char from) { int i; i = 0; while((to[i] = from[i]) != '') i++; } Not the problem, but you ...

Sending the same but modifed object over ObjectOutputStream

Sending the same but modifed object over ObjectOutputStream I have the following code that shows either a bug or a misunderstanding on my part. I sent the same list, but modified over an ObjectOutputStream. Once as [0] and other as [1]. But when I read it, I get [0] twice. I think this is caused by the fact that I am sending over the same object and ObjectOutputStream must be caching them somehow. Is this work as it should, or should I file a bug? 3 Answers 3 The stream has a reference graph, so an object which is sent twice will not give two objects on the other end, you will only get one. And sending the same object twice separately will give you the same instance twice (each with the same data - which is what you're seeing). See the reset() method if you want to reset the graph. Max is correct, but you can also use: public void writeUnshared(Object obj); See comment below for caveat ...

Difference Between flush() vs reset() in JAVA

Difference Between flush() vs reset() in JAVA I was just wondering what the difference between flush and reset is? Why is it using reset after flush in example? Why is reset method used if memory cache is wiped by flush method? ObjectOutputStream oos = new ObjectOutputStream(bos); while(true){ oos.writeObject(object); oos.flush(); oos.reset(); object.x++; } 3 Answers 3 Why is reset method used if memory cache is wiped by flush method? flush() will write the buffer of the ObjectOutputStream object in the underlying OutputStream but it will not reset the whole state of the ObjectOutputStream object. flush() ObjectOutputStream OutputStream ObjectOutputStream If you open the ObjectOutputStream source code class, you can see that beyond the buffer it contains many instance fields. Here is a little snippet : ObjectOutputStream /** filter stream for handling block data conversion */...