C++: File Size

Bjarne-stroustrup
 

In this task, the job is to verify the size of a file called “input.txt” for a file in the current working directory and another one in the file system root.

#include <iostream>
#include <fstream>

std::ios::off_type getFileSize(const char *filename) {
	std::ifstream f(filename);
	std::ios::pos_type begin = f.tellg();
	f.seekg(0, std::ios::end);
	std::ios::pos_type end = f.tellg();
	return end - begin;
}

int main() {
	std::cout << getFileSize("input.txt") << std::endl;
	std::cout << getFileSize("/input.txt") << std::endl;
	return 0;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!