How to resolve the algorithm Check that file exists step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Check that file exists step by step in the C++ programming language

Table of Contents

Problem Statement

Verify that a file called     input.txt     and   a directory called     docs     exist.

This should be done twice:

Optional criteria (May 2015):   verify it works with:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Check that file exists step by step in the C++ programming language

Explanation of the First Source Code:

This C++ code uses the boost::filesystem library to check the existence and nature of files and directories.

  • Function testfile:

    • This function takes a file or directory name as a string parameter.
    • It creates a boost::filesystem::path object from the name.
    • It checks if the path exists using the exists function.
    • If the path exists, it checks if it's a directory using the is_directory function and prints an appropriate message.
  • Main Function:

    • It calls testfile with various file and directory names:
      • "input.txt" (a non-directory file)
      • "docs" (a directory)
      • "/input.txt" (a full path to a non-directory file)
      • "/docs" (a full path to a directory)

Explanation of the Second Source Code:

This C++ code uses the C++20 Filesystem Library to perform similar file and directory checks.

  • Function file_exists:

    • This function takes a std::filesystem::path object as a parameter.
    • It prints the path.
    • It checks if the path exists using the exists function.
    • If the path exists, it checks if it's a directory using the is_directory function and prints an appropriate message.
    • If the path is not a directory, it prints the file size using the file_size function.
  • Main Function:

    • It calls file_exists with various file and directory paths:
      • "input.txt" (a non-directory file)
      • "zero_length.txt" (an empty file)
      • "docs/input.txt" (a full path to a non-directory file)
      • "docs/zero_length.txt" (a full path to an empty file)

Source code in the cpp programming language

#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");
}


#include <iostream>
#include <filesystem>

void file_exists(const std::filesystem::path& path) {
    std::cout << path;
    if (  std::filesystem::exists(path) ) {
    	if ( std::filesystem::is_directory(path) ) {
    		std::cout << " is a directory" << std::endl;
    	} else {
    		std::cout << " exists with a file size of " << std::filesystem::file_size(path) << " bytes." << std::endl;
    	}
    } else {
        std::cout << " does not exist" << std::endl;
    }
}

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


  

You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Main step of GOST 28147-89 step by step in the REXX programming language
You may also check:How to resolve the algorithm Eban numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Maple programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Wren programming language