Please take a look at this
Code:
fstream* file1 = new fstream("C:\\Testfile.sia", ios_base::out);
file1->close();
file1->open("C:\\Testfile.sia");
cout << file1->tellp() << endl;
cout << file1->tellg() << endl;
file1->write("somethingsinlifetheyjustdon'wannasee",30);
file1->seekp(10);
file1->seekg(5);
cout << file1->tellp() << endl;
cout << file1->tellg() << endl;
file1->close();
What is the difference between seekp() and seekg()? They seem to always moving together.
tellg(): Reports the current read position in the stream.
tellp(): Report position in output stream.
seekp(): Reset position in output stream.
seekg(): Moves the read position in a stream.
[edit] I was ... . I just don't know why they move together.
I'm not sure why MSN has it explained like that. It's not incorrect, but perhaps confusing.
seekg sets the
get
pointer of the stream to a new position.
seekp sets the
put
pointer of the stream to a new position.
seekg is in fact istream::seekg
seekp is in fact ostream::seekp
Well conceptually they are not. But in practice they are. The thing is that despite opening a file for read and write access there is only
one
marker to that buffer. Both seekg and seekp point to this marker.
Both functions (and the accompanying tell functions) make only sense to be used together when you open a stream for reading and writing
and
that stream allows for random access.
You can read and write to the same stream without needing to close and reopen it each time. You read from the stream starting from positions you indicate with seekg and you write to positions you indicate with seekp.
fstream is a random access stream. You can open an with with
fstream::in | fstream:: out
. seekp and seekg will allow you to freely navigate the stream up and down both for reading and writting.
Of course, you cannot expect to make any sense of the value tellg or tellp return. These functions return a streampos object. This class is used by the compiler (and is compiler specific) in order to establish the positions in the stream. But otherwise it has no meaning to us humans. You can add or subtract two streampos objects. This allows you to do relative searches in addition to random ones.
I'm sure. But if they are implemented differently there is certainly a very good reason to. The answer lies definitely in the way streams are implemented. Probably something to do with input and output sequences.
I cannot help you much further than this. I simply don't know past what I already told you. I tend not to ask many questions on how this or that is implemented. I simply accept it and move on. May seem as the antithesis of the learning process. But it serves me well. When the programming language is well known to me and I'm well adapted at coding complex applications with it, then definitely I will be more interested in knowing how it all was put together.
C++ Tutorial
5 ways you can learn to program faster
The 5 Most Common Problems New Programmers Face
How to set up a compiler
8 Common programming Mistakes
What is C++11?
Creating a game, from start to finish
Recent additions
How to create a shared library on Linux with GCC
- December 30, 2011
Enum classes and nullptr in C++11
- November 27, 2011
Learn about The Hash Table
- November 20, 2011
Rvalue References and Move Semantics in C++11
- November 13, 2011
C and C++ for Java Programmers
- November 5, 2011
A Gentle Introduction to C++ IO Streams
- October 10, 2011