seekp & seekg not pointing same value..

Hi..
seekg and seekp are giving different values from the below program.. Pl someone help by explaining what is going on here because of getline function presence..

And unable to understand why tellg() giving -1. As per my knowledge, a pointer value should never become a negative.. But, here it is going to -1. Pl clarify..



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 

int main ()

{
  fstream temp ("temp.txt", ios::in | ios:: out ) ;
  
  temp.seekp (0,ios::beg);

  char arr[]="Bye bye.. Agatti";
  char arr2[]="Hello.. Chennai";

  temp.write ((char *) & arr, sizeof arr);
  temp.write ((char *) & arr2, sizeof arr2);

   temp.seekp(0);
   cout <<"Tellp value is : "<< temp.tellp()<< endl;
   cout <<"Tellg value is : "<< temp.tellg()<< endl<<endl;


  // reading content of a file by getline() through fstream object
  char strinn[100];
  temp.getline (strinn, 100);
  cout <<strinn <<endl;
  cout << (strinn+17) <<endl<<endl;

  
 cout <<"Tellp value is : "<< temp.tellp()<< endl;
 cout << "Tellg value is : "<<temp.tellg() <<endl;  

  temp.close();

  return 0;
}



Output :
Tellp value is : 0
Tellg value is : 0

Bye bye.. Mumbai
Hello.. Chennai

Tellp value is : 33
Tellg value is : -1
-1 is used to represent that there is no more characters to be read. i.e. the eof character has been extracted.
Understood now.. if we use temp.clear() , then seekg also becoming location of last byte (33, for given program).. Many thanks smac89.. ;)
Topic archived. No new replies allowed.