C++: Check That File Exists

Bjarne-stroustrup
 

In this task, the job is to verify that a file called “input.txt” and the directory called “docs” exist. This should be done twice: once for the current working directory and once for a file and a directory in the filesystem root.

#include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
	boost::filesystem::path file(name);
	if (exists(file))
	{
		if (is_directory(file))
		std::cout << name << " is a directory.\n";
		else
		std::cout << name << " is a non-directory file.\n";
	}
	else
	std::cout << name << " does not exist.\n";
}

int main()
{
	testfile("input.txt");
	testfile("docs");
	testfile("/input.txt");
	testfile("/docs");
}

SOURCE

Content is available under GNU Free Documentation License 1.2