-1
#include<fstream>
#include<iostream>
using namespace std;
int main()
{   ifstream fin;
    fin.open("pro.txt");
    char ch;
    while(!fin.eof())
    {
      fin.get(ch);
      cout<<ch;
    }
    fin.seekg(0);
    int pos=(int)fin.tellg();
    cout<<"\n pointer is at :"<<pos;
    fin.close();
    return 0;
}

content of file pro.txt

this is the test file text

even after fin.seek(0) the position of get_pointer returned by fin.tellg() is always -1 (WHY ?). it happens only when i read the file till the eof(). HELP

output

this is the test file text
pointer is at: -1
akitsme
  • 41
  • 11
  • 3
    Related: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – drescherjm Aug 25 '16 at 16:26
  • 1
    Are you sure you are reading the file at all? Make sure it is in the correct folder. If this is Visual Studio the default folder is the one containing the project file (unless you changed the debug settings for your project). Also you should update your code to warn the user if the file is not found. – drescherjm Aug 25 '16 at 16:28
  • thnx but it will be better if you copy my code and check youself, – akitsme Aug 25 '16 at 16:35
  • i know how to read and write files – akitsme Aug 25 '16 at 16:36
  • You forgot to [`clear()`](http://en.cppreference.com/w/cpp/io/basic_ios/clear) your streams failure state prior to seeking back to the beginning. – WhozCraig Aug 25 '16 at 16:36
  • there is no problem in the file reading , file reading is correct the only wrong value is of fin.tellg – akitsme Aug 25 '16 at 16:37
  • well you can make your own text file with same name – akitsme Aug 25 '16 at 16:38

2 Answers2

1

From documentation:

C++98: If the eofbit flag is set before the call, the function fails (sets failbit and returns).

If you do fin.clear() before seekg, it will work.

KIIV
  • 3,239
  • 2
  • 15
  • 20
1

Once your stream state hits failure (and it did) any stream operations, including seeking, will fail until that state is cleared.

Example (using string stream):

#include <iostream>
#include <sstream>

int main()
{
    std::istringstream iss("Some text to read");
    char ch;
    while (iss.get(ch))
        std::cout << ch;
    std::cout << '\n';

    iss.clear(); // <<==== HERE
    iss.seekg(0, std::ios::beg);

    auto pos = iss.tellg();
    std::cout << "pos = " << pos << '\n';

    while (iss.get(ch))
        std::cout << ch;
    std::cout << '\n';
}

Output

Some text to read
pos = 0
Some text to read
WhozCraig
  • 59,130
  • 9
  • 69
  • 128
  • which language is this ? – akitsme Aug 25 '16 at 16:44
  • @AkashKabir The code ? It's C++, compiled and tested using clang 3.8 in `-std=c++11` mode. Is it the `auto` that is throwing your compiler off ? if so, use `std::ios::pos_type pos = ...` instead. [See example here](http://ideone.com/oDKO3A). – WhozCraig Aug 25 '16 at 16:48
  • what is isstringstream ? is it compiler specific? – akitsme Aug 25 '16 at 16:55
  • No, its a string stream (as the example states), brought in by ``. Read more about it [**here**](http://en.cppreference.com/w/cpp/io/basic_istringstream) (and bookmark that site, as it is the best online resource for C++ you're likely to find). – WhozCraig Aug 25 '16 at 17:07