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.
-
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.
Read the documentation The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.) See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully. In later versions of Visual Studio they use different terms seekg => The absolute position in which to move the read pointer. seekp => The position in the stream (meaning both read and write) You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set
In vino veritas
-
Read the documentation The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.) See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully. In later versions of Visual Studio they use different terms seekg => The absolute position in which to move the read pointer. seekp => The position in the stream (meaning both read and write) You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set
In vino veritas
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.
-
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.
First you have the input and output pointers mixed in the above Second the put function doesn't work the way you think in the above MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer. Put in.sync() after the put MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program. I think you need to do some more reading on streams with the sync it will do what you expect.
In vino veritas
-
First you have the input and output pointers mixed in the above Second the put function doesn't work the way you think in the above MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer. Put in.sync() after the put MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program. I think you need to do some more reading on streams with the sync it will do what you expect.
In vino veritas
Thanks.