tellg() won't return the size of the file, it returns -1.
What is the reason for it?
Code:#include <string>
#include <iostream>
#include <fstream>
using namespace std;
fstream file;
int main()
{
// Creat file
ofstream outfile("test.txt");
outfile.close();
string str ;
file.open("test.txt", fstream::in | fstream::out);
if (!file.is_open())
{
cerr << "Error opening file." << endl;
return 1;
}
file << "Hello World." << endl;
file.seekg (0, ios::beg);
while(!file.eof())
{
getline(file, str);
cout << str << endl;
}
file.seekg(0, ios::end);
int newsize = file.tellg();
file.seekg(0, ios::beg);
cout << newsize << endl;
file.close();
return 0;
}