What is the difference between seekg() and seekp(), and tellg() and tellp(), as they always appear to be returning the same values ?
Printable View
What is the difference between seekg() and seekp(), and tellg() and tellp(), as they always appear to be returning the same values ?
The 'g' version move the get pointer, the 'p' versions move the put pointer... If memory serves, a particular implementation may use a single pointer for both. However if you are about to perform input you should definitely use seekg(), and seekp() in the other case.
seekg moves the file input pointer(position of reading frm file) while seekp moves file output pointer( position f writing to file).
Another question from juice answerable simply by reading the documentation.
istream::seekg - C++ Reference
ostream::seekp - C++ Reference
And had you read the documentation you would have seen that seekg and seekp can not be used interchangeably....
This is what makes me think so...
you really think I would like to post a question and wait for someone to answer when I could simply get an answer from some documentation?Code:#include"stdafx.h"
#include<iostream>
#include<fstream>
struct record
{
char code[6];
char name[20];
int i;
}r;
int main()
{
std::fstream file("Temp.dat",std::ios::trunc|std::ios::in|std::ios::out|std::ios::binary);
if(!file)
{
std::cout<<"unable to open file";
exit(0);
}
std::cout<<"enter character code, name and an int\n";
std::cin.getline(r.code,6);
std::cin.getline(r.name,20);
std::cin>>r.i;
file.write((char *)&r,sizeof(r));
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
file.seekg(3);
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
file.seekp(5);
std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
}
Just because seekg and seekp may operate on the same pointer (the get and put pointer) they are not guaranteed to do so, which you would have realized had you read the two links posted to you....
By that same logic, writing outside of the bounds of an array must be okay because this works (for me):Quote:
Originally Posted by juice
Code:#include <stdio.h>
int main(void) {
int a[10];
a[12] = 1;
printf("%d\n", a[12]);
return 0;
}
Ah, the familar sequence of steps of someone who does not really want to learn...
Asks Question instead of looking it up.
Be given correct answer anyway.
Ignores answer and decides their own wrong answer is correct.