Path Name


Program Listing

PathName.cpp

/************************************************************************
*                                                                       *
*   Program:    PathName                                                *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Get full pathname of source file

    cout << "Enter the complete pathname of the source file:" << endl << endl;
    string PathName;
    cin >> PathName;

//  See if file name ends with .cpp

    int len = PathName.size();
    if (PathName.substr(len - 4, 4) != ".cpp") {
        cout << "Source file name must end with '.cpp'" << endl;
        cout << "Exiting..." << endl;
        exit(1);
    }

//  Get output file name

    cout << "Enter the output file name:  " << flush;
    string OutFileName;
    cin >> OutFileName;
    
//  Separate source file name from path

    int pos = PathName.rfind(":") + 1;
    string SrcFileName = PathName.substr(pos, PathName.size() - pos);
    string Directory = PathName.substr(0, pos - 1);

//  Append output file name to path

    string OutPathName = Directory + ':' + OutFileName;

//  Convert output file pathname to DOS

    string DOSPathName = OutPathName;
    pos = DOSPathName.find(":");
    while (pos > 0) {
        DOSPathName[pos] = '\\';
        pos = DOSPathName.find(":");
    }

//  Print the results

    cout << endl << "The source file name is " << endl;
    cout << endl << '\t' << SrcFileName << endl << endl;
    cout << "The directory is " << endl;
    cout << endl << '\t' << Directory << endl << endl;
    cout << "The output file pathname is " << endl;
    cout << endl << '\t' << OutPathName << endl << endl;
    cout << "The DOS output file pathname is " << endl;
    cout << endl << '\t' << DOSPathName << endl << endl;
    
    return 0;
}

Sample Run

Enter the complete pathname of the source file:

MacintoshHD:CIA_Files:KennedyAssassination:TheCulprit.cpp
Enter the output file name:  TheCulprit.out

The source file name is 

    TheCulprit.cpp

The directory is 

    MacintoshHD:CIA_Files:KennedyAssassination

The output file pathname is 

    MacintoshHD:CIA_Files:KennedyAssassination:TheCulprit.out

The DOS output file pathname is 

    MacintoshHD\CIA_Files\KennedyAssassination\TheCulprit.out



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:35 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/PathName.html

Return to Coms 261 home page