The Wayback Machine - https://web.archive.org/web/20220816042724/https://www.geeksforgeeks.org/cpp-program-to-compare-paths-of-two-files/
Skip to content
Related Articles

Related Articles

C++ Program to Compare Paths of Two Files

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 04 Aug, 2022

As we are given two paths of two files, we have to compare these two paths and check whether they are equal or greater or smaller using a C++ program.

Input:

path1 = "/a/b/c"    , path2 = "/a/b/"

Output:

path1 is greater than path2

Approach:

  1. To store paths use string as data type 
  2. Use pathname1. Compare( pathname2 ), to compare two paths , this will return three values  greater that 0 , less than 0 or equal to 0 

Example:

C++




// C++ Program to Compare Paths of Two Files
#include <iostream>
using namespace std;
 
// function to compare two paths
void pathCompare(string p1, string p2)
{
 
    // stores compared value 0 or >0 or <0
    const int res = p1.compare(p2);
    if (res > 0)
        cout << p1 << " is greater than " << p2;
    else if (res == 0)
        cout << p1 << " is equal to " << p2;
    else
        cout << p1 << " is less than " << p2;
    cout << "\n";
}
 
// Driver code
int main()
{
    string p1 = "/a/b/c";
    string p2 = "/a/b/";
    string p3 = "/a/b";
    string p4 = "/a/b";
    string p5 = "/a/b";
    string p6 = "/a/b.";
    pathCompare(p1, p2); // function call
    pathCompare(p3, p4); // function call
    pathCompare(p5, p6); // function call
    return 0;
}

Output

/a/b/c is greater than /a/b/
/a/b is equal to /a/b
/a/b is less than /a/b.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!