Directory?

This is a discussion on Directory? within the C++ Programming forums, part of the General Programming Boards category; ok heres my prob... im letting the user enter a file name for the program to open for output ( ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Question Directory?

    ok heres my prob...
    im letting the user enter a file name for the program to open for output ( to view )...but it only opens files from my compilers directory. I want the user to specify the directory and then be able to type the name of the file. it doesnt matter if its like a menu to choose the directory or "C:\data.doc" (DOS style) but i cant seem to find out how to do this. can anyone help me out??

    this is what i am usin to open the file

    ...
    cout << "Enter the name of the output file: \n";
    cin.get(outfile,13);
    cin.ignore(80,'\n');

    outfile.open(outputfile, ios:ut);
    ....

    thx everyone!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    take a look at the <direct.h> file

    and at the directory functions

    _chdir ...to change working directory
    _getcwd .. to get the current working directory
    zMan

  3. #3
    Unregistered
    Guest

    zMan

    ok zMan.. its like DOS commands... how would this tie into my basic programm though? i mean how do i use those commands in my program? like lets make this easy.. if i was goin to ask the user for the directory then what file he/she would like to open how would i use those commands to accomplish that? thx again.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    try this program....

    Code:
    #include<iostream>
    #include<string>
    #include<direct.h>
    
    using namespace std;
    
    
    string strFile;
    void ChangeDir( void );
    void EnterFileName( void );
    void DisplayCurrentFile( void );
    void ChangeDrive();
    
    void PrintMenu(void);
    
    void main( void )
    {
    	int iEntry = -1;
    
    	while( 1 )
    	{
    		PrintMenu();
    
    		cin >> iEntry;
    		
    		switch( iEntry )
    		{
    		case 0: ChangeDrive(); break;
    		case 1: ChangeDir();     break; 
    		case 2: EnterFileName(); break;
    		case 3: DisplayCurrentFile(); break;
    		}
    
    		if( iEntry == 4)
    			break;
    	}
    }
    
    void PrintMenu(void)
    {
    	cout << "\n Main Menu";
    	cout << "\n------------------------";
    	cout << "\n0. Change drive";
    	cout << "\n1. Change directory";
    	cout << "\n2. Enter file name";
    	cout << "\n3. Display";
    	cout << "\n4. Exit Program";
    	cout << "\n" << endl;
    }
    
    void ChangeDir( void )
    {
    	//** example: C:\mydir\newdir\somedir
    	char dir[80] = "";
    	cout << "\nEnter new directory: " << flush;
    	cin >> dir;
    	if(_chdir( dir ) )
    		cout << "\n" << dir << "is not a valid dir" << endl;
    }
    
    void EnterFileName( void )
    {
    	//**example myfile.txt
    	char buffer[80] = "";
    	cout << "\nEnter file name: " << flush;
    	cin >> buffer;
    
    	strFile = buffer;
    }
    
    //** concatenates the dir and the file
    //** example dir = "c:\mydir
    //**         file = "myfile.txt"
    //** displays c:\\mydir\\myfile.txt
    //** 
    void DisplayCurrentFile( void )
    {
    	char buffer[_MAX_PATH];
    	string strFullPath;
    
       /* Get the current working directory: */
       if( _getcwd( buffer, _MAX_PATH ) == NULL )
          perror( "_getcwd error" );
       else
       {	
    	   strFullPath = buffer;
    	   strFullPath += "\\";
    	   strFullPath += strFile;
    	   cout << strFullPath << endl;
    	}
    	
    }
    
    //** drives are specified to _chdrive as int values 1-n where n is 
    //** your last drive
    void ChangeDrive()
    {
    
    	char chDrive[10] = "";
    	cout << "\nEnter drive letter: " << flush;
    	cin >> chDrive;
    	chDrive[0] = toupper( chDrive[0] );
    
    	if( _chdrive( chDrive[0] - 'A'+1) )
    		cout << "Invalid drive: " << (int)chDrive[0]-'A'+1<< endl;
    }
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to tell if a file or directory
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2009, 10:57 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 08:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21