You are using seekg incorrectly. You are using the values of the constants ios::beg and ios::end as the offsets! Presumably their values are 0 for beg and 2 for end. Hence your result. https://en.cppreference.com/w/cpp/io/basic_istream/seekg
// streamtest.cpp
#include <fstream>
#include <iostream>
int main()
{
std::fstream file( "streamtest.cpp", file.in | file.binary);
if (!file ) {
std::cerr << "streamtest.cpp couldn't opened.";
return 1;
}
file.seekg(0, file.end); // you only need the end position to determine the size
auto size = file.tellg();
std::cout << "size: " << size << '\n';
// you need to seek back to the start before the read
file.seekg(0, file.beg);
auto buf = newchar[size];
file.read( buf, size);
file.close();
file.open( "streamtest.out.cpp", file.out | file.binary);
file.write( buf, size);
delete buf;
}