Tellg () ???

Hi I am writing a program and I used function tellg I thought tellg () returns curent possition of curssor in file but this is not true. Letter I try one simple program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string line
int main ()
{
fstream file;
file.open ("z.txt");
file.seekg (0);
while (1)
{
file >> line;
cout << line.length ()<< endl;
cout << "line "<< line << endl; 
cout <file.tellg ()<< endl;
if(file.good ()!=true)break;
};
cout << "\n";
system ("PAUSE");
}

why if is in z.txt file text "abc"file.tellg () do not return same as line.length ()?
Please can anyone explain
Because the member function "string::length()" returns the number of characters in the string where as the member function "ifstream::tellg()" tells you the current position of the pointer in that stream.

Source:
http://www.cplusplus.com/reference/iostream/istream/tellg/
http://www.cplusplus.com/reference/string/string/length/

EDIT: These are member functions, meaning the data they return is somehow related to or based on the variable that they are attached to. So in the case of "length()" it was attached to the string "line", anything to do with the ifstream "file" is outside of it's scope.
Last edited on
Topic archived. No new replies allowed.