I wrote simple function that read whole file into a buffer.
#include <iostream>
#include <fstream>
int main()
{
std::ios_base::sync_with_stdio(0);
std::ifstream t;
t.open("C:\\Users\\sufal\\Desktop\\test.txt");
t.seekg(0, std::ios::end);
long length = t.tellg();
t.seekg(0, std::ios::beg);
std::cout << "file size: " << length << std::endl;
char* buffer = new char[length+1];
t.read(buffer, length);
t.close();
buffer[length] = 0;
std::cout << buffer << std::endl;
return 0;
}
And this is test.txt:
1
2
3
The output that the program produces looks like this:
The file size should be 5 bytes. Why my program shows wrong file size? Windows Explorer also seems to show wrong file size of 7 bytes.