Allowing User To Select Directory..

This is a discussion on Allowing User To Select Directory.. within the C++ Programming forums, part of the General Programming Boards category; In this function, I'm trying to allow the user to select which directory the mp3 playlist is saved to.. Code: ...

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Thumbs up Allowing User To Select Directory..

    In this function, I'm trying to allow the user to select which directory the mp3 playlist is saved to..

    Code:
    void savePlaylist()
    {
    	string directoryToSaveTo;
    
    	cout << "What directory would you like to load a playlist from?" << endl
    		 << "The file must be a file named playlist.wpl" << endl
    		 << "Example:  C:\\" << endl;
    	
    	cin >> directoryToSaveTo;
    	cout << endl << endl;
    
    	directoryToSaveTo = directoryToSaveTo + "playlist.wpl";
    
    	ofstream playlist;
    	playlist.open(directoryToSaveTo); //compiler error here, the open function is
                                                                             //supposed to be used like this: 
                                                                             //playlist.open("c:\playlist.wpl");
    	
    	playlist << "<?wpl version=\"1.0\"?>" << endl
    			 << "<smil>" << endl
    			 << "    <head>" << endl
    			 << "        <meta name=\"Generator\" content=\"Microsoft Windows Media Player -- 10.0.0.4036\"/>" << endl
    			 << "        <author/>" << endl
    			 << "        <title>Playlist</title>" << endl
    			 << "    </head>" << endl
    			 << "    <body>" << endl
    			 << "        <seq>" << endl;
    			 for(int j = 1; fileAddresses[j] != ""; j++)
    	playlist << "            <media src=\"" << fileAddresses[j] << "\"/>" << endl;
    	playlist << "        </seq>" << endl
    			 << "    </body>" << endl
    			 << "</smil>" << endl;
    
    	playlist.close();
    	cout << "\nPlaylist saved to c:\\playlist.wpl" << endl;
    	quitMessage();
    }
    I want to be able to have the user select where the playlist gets saved, but I get the compiler error:

    cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
    What can I do to get around this problem?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    You're better of with char;

    Code:
    int main()
    {
    
    char directoryToSaveTo[255];
    
    	std::cout << "What directory would you like to load a playlist from?" << endl
    		 << "The file must be a file named playlist.wpl" << endl
    		 << "Example:  C:\\" << endl;
    	
    	std::cin >> directoryToSaveTo;
    	std::cout << endl << endl;
    
    	strcat (directoryToSaveTo,"playlist.wpl");
    
    	ofstream playlist;
    	playlist.open(directoryToSaveTo); //no compiler error here ;)
    
    
    }
    Last edited by Oldman47; 12-13-2006 at 08:11 AM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    the open() method of the playlist object expects const wchar_t*. A pointer to a const wchar_t, which on this case almost certainly refers to a const array of wchar_t.

    The string object cannot convert automatically to an array of wchar_t.
    I'm not used to wide character arrays. However I think it will be enough if you pass it directoryToSaveTo.str(), which will pass a C-Style string and I reckon the implicit conversion to a wchar_t array will happen.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    it is called c_str()
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    c_str(), yes. Mark that.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > You're better of with char;
    Sticking with std::string and learning how to use it properly is better IMO.
    Rushing back to unsafe C char arrays isn't the way to go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thanks guys. Used c_str() and it worked fine.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Where did wchar_t make it into the mix? Even wide streams take narrow-char filenames.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    from this:
    cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, but how did the compiler come up with this message? There is no open(const wchar_t*) member function in basic_ifstream.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    It happens when playlist.open() gets called with a std::string parameter. My guess is that this is someone else's library.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Maybe open() is overloaded to accept wchar_t* and char*.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Hmm, apparently there are two things working together.
    First, the MS standard library provides wchar_t overloads of open().
    Second, the MS compiler produces really stupid error messages. (It should offer all overloads if it can't call any.)

    I see ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A fifo file and select() problem
    By itsdafoetime in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 01:23 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:15 AM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 12:21 PM
  5. The Site Directory
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-22-2002, 07:19 PM

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