Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get file size with ifstream::seekg and tellg

Tags:

c++

c

I have the following block to test the behaviours of the seekg and tellg when I try to get a file size:

    int size = 0;
    ifstream in(fileName.c_str(), ifstream::in | ifstream::binary);
    if(in)
    {
        in.seekg(0,ifstream::end);
        size = in.tellg();
        cout << endl;
        cout << endl;
        cout << "********** size stream1*** =" << size << endl;
        in.seekg(0,ios::end);
        size = in.tellg();
        cout << "********** size stream2*** =" << size << endl;
        in.seekg(0,ios::end);
        size = in.tellg();
        cout << "********** size stream3*** =" << size << endl;
        in.seekg(100,ios::end);
        size = in.tellg();
        cout << "********** size stream4*** =" << size << endl;
        in.seekg(0,ios::beg);
        size = in.tellg();
        cout << "********** size stream5*** =" << size << endl;
        in.seekg(100);
        in.seekg(0, ios::end);
        size = in.tellg();
        cout << "********** size stream6*** =" << size << endl;
        in.seekg(100);
        in.seekg(0, ios::cur);
        size = in.tellg();
        cout << "********** size stream7*** =" << size << endl;
        in.seekg(-100,ios::end);
        size = in.tellg();
        cout << "********** size stream8*** =" << size << endl;
        in.seekg(ios::beg,ios::end);
        size = in.tellg();
        cout << "********** size stream9*** =" << size << endl;
        in.seekg(ios::beg);
        in.seekg(ios::end);
        size = in.tellg();
        cout << "********** size stream10*** =" << size << endl;
        cout << endl;
        cout << endl;
    }

Its results are followed:

********** size stream1*** =1846
********** size stream2*** =1846
********** size stream3*** =1846
********** size stream4*** =1946
********** size stream5*** =0
********** size stream6*** =1846
********** size stream7*** =100
********** size stream8*** =1746
********** size stream9*** =1846
********** size stream10*** =2

My questions are the following:

  1. does the tellg will return the bytes the last seekg found?
  2. Why the stream5 result is 0? its previous position is at the end of the file.
  3. seekg will always start at beginning of the file as shown in stream5?
  4. seekg cannot go backward as shown in stream5?
  5. what's the first parameter of seekg really mean? It says it is the offset of the second parameter. It seems it goes beyond the position pointed by the second parameter if it is a positive value, as showing in stream4.
  6. If I want to know the size after the first 100 bytes, I have to use the -100 as shown in stream8?
  7. why stream10 is 2?
  8. seekp and tellp in ofstream have the properties as seekg and tellg here?
like image 430
5YrsLaterDBA Avatar asked Sep 05 '25 16:09

5YrsLaterDBA


1 Answers

OK, here we go:

  1. No. tellg() will yield the position object useful to get back to the position you are at. The type returned is a std::streampos which is std::fpos<std::mbstate_t>. However, an std::fpos<std::mbstate_t> converts to a std::streamoff which is an integer. Essentially, a std::streampos converts to the number of characters from the first position to the current position.
  2. Seeking to 0 characters relative to std::ios_base::beg clearly sets the position to the start of the file. Did you mean to use std::ios_base::cur?
  3. No. seekg() will be relative to the start, current position, or the end depending on the whence parameter being std::ios_base::beg, std::ios_base::cur, or std::ios_base::end.
  4. No. You can go backward using a negative offset.
  5. See 3.: the first parameter is the offset being moved relative to the location specified by the whence parameter.
  6. No. If you know how many characters are in the file, you subtract 100 from that number.
  7. You used absolute positioning using the whence values as position. The std::ios_base::seekdir type is an enumeration and std::ios_base::end probably has the value 2.
  8. Pretty much.
like image 165
Dietmar Kühl Avatar answered Sep 07 '25 10:09

Dietmar Kühl