Thanks.
Biruk Abebe
Posts
-
C++ file stream read/write[SOLVED] -
C++ file stream read/write[SOLVED]Thanks for your replay. But the code doesn't assume the input and output pointers are independently moved. In fact,in this case, the put() method will move the input pointer and the get() method move the output pointer as it is evident by looking at the values returned from tellp() and tellg(). This is why i am confused. if the put() statement in the code moves the input file pointer to the next character( which will be read by the next iteration) why do i need to seek to it explicitly is the question.
-
C++ file stream read/write[SOLVED]I need to open a file for both reading/writing using fstream and read each character then write that character back to the file. for example i have this code.
fstream in("test.txt",ios::in | ios::out); if(!in) cout<<"error..."; else { char ch; in.seekg(0,ios::end); int end=in.tellg();//get the length in.seekg(0);//get back to the start for(int i=0;i(in.tellg())-1);//move the pointer back to the previously read position,so i could write on it if(in.put(ch).fail())//write back,this also move position to the next character to be read/write break;//break on error } } }
I have a file named "test.txt" which contains "ABCD". As i understand it both put() and get() methods of the stream object move the file pointer forward(i see that by getting the return value of tellg() or tellp() functions after each get() or put() method call). My question is when i comment out the code that will seek the stream pointer to "where it is now"(in.seekg(in.tellg()), the code will result incorrect results. I don't understand why this is since tellg() is showing the correct position of the character to be read next what is the purpose of explicitly seeking to it? The incorrect result is it write to the file "ABBB" instead of "ABCD". I am using visual studio 2005.