seekp and seekg return a reference to the stream. You should be using tellp and tellg to output the position of the put and get pointers, respectively.
I use tellg and tellp to output the position flag.
However they always output the same value.
Imagine the following code:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
fstream file;
file.open("some.bin", ios::out | ios::in | ios::binary);
file.seekp(3);
file.seekg(300);
int a=file.tellg;
int b=file.tellp;
}
In this case a==b, while they should be: a=300 and b=3. Right?
EDIT: Another question, if you're willing to anwser:
How can I tell if a file was created before. Imagine I want to run some statements only the first time I open a file and the program might be run a number of times.
when you open a fstream with in | out, VS converts it to a "r+" inside its code. Now r+ will only work if the file already exists. You can do a stepin to see that. So this may be the problem if you are on windows.
so either you give a in or out or if you want to read and write, give a ios::app also.
Essentially, a stream only maintains one pointer. Since you're dealing with one file, the put and get pointers will always point to the same location. If you reversed your seeking above, that is, if you did:
1 2
file.seekg(2);
file.seekp(3);
then your program will output 3 for both.
Now, if you were reading from one file and writing to another it would be different. You could seekg in the in-file (and tellg would report that correctly) and you could seekp in the out-file (and tellp would report that correctly). Note that you can't call seekp/tellp on an instance of ifstream and you can't call seekg/tellg on an instance of ofstream. However, you can use both on an instance of fstream.
If you want to be reading and writing to the same file with seeking, you need to maintain your own get and put pointers, and seek to those (and update them as needed).