CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Posts
    47

    Save/load file windows

    Afternoon everyone.
    Basically, I want my program to bring up the generic windows save/load boxes. And i have no idea really how to go about doing it.
    The code below is whats used to create the file that i want to save, its pretty long, so i cut out bits that you dont really need to know:
    Code:
            ofstream saveFile;
    	saveFile.open("Save.mbl");  //open file
    
    	saveFile << polyphony << endl; 
    
    	for(int i = 0; i < polyphony; i++)  //for each voice
    	{
    		saveFile << voices[i].automata->GetRule() << endl;  //write out the CA rule
    		saveFile << voices[i].parameters[47].value << endl;
                    //etc etc - write out a bunch of boring stuff
    
    	}
    	for(int i=0; i<polyphony; i++)
    	{
    		//and some more numbers
    	}
    
    	saveFile.close();
    What i then need to happen, is the standard dialog box to come up, and for the user to choose where to save the file.
    Thanks in advance for any help with this.
    -M

  2. #2
    Join Date
    Apr 2007
    Posts
    162

    Re: Save/load file windows

    If you are using MFC read up on CFileDialog.
    If not MFC check out GetSaveFileName and GetOpenFileName.
    Last edited by zapper222; November 14th, 2011 at 10:20 AM.

  3. #3
    Join Date
    Feb 2011
    Posts
    47

    Re: Save/load file windows

    Im not. So i did read up on GetSaveFileName before coming here, which lead me to:

    BOOL WINAPI GetSaveFileName(
    __inout LPOPENFILENAME lpofn
    );

    But this is gonna sound ridiculously noobie, i dont have a clue what to do with it. A poke in the right direction?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Save/load file windows

    Quote Originally Posted by Medic1de View Post
    But this is gonna sound ridiculously noobie, i dont have a clue what to do with it.
    So I'll ask you -- how did you expect to open a "save as" dialog in code? You would need to call a function somewhere to do this work, and that is what GetSaveFileName() does --it's a function -- you call it, no different than any other function.

    The parameter you pass to it is a structure that is filled with information on how the "save as" dialog is to work (filter, starting directory, etc.)

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://www.winprog.org/tutorial/app_two.html

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2011
    Posts
    47

    Re: Save/load file windows

    You're a boss. Thanks so much. Thats pretty much exacxtly what i needed. So i've using adapted that a bit, and i've got another stupidly simple question thats giving me trouble.
    I've got this line thats used when saving/naming the file:
    saveFile.open(FileName);

    And i basically need:
    saveFile.open(FileName+".lbm"); //instead of having no file type

    Filename is an array of chars, simelar to in the example. Again, im sure this is simple, but i cant remember how the hell to do it.
    Either way, thanks for your help man x

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Save/load file windows

    Quote Originally Posted by Medic1de View Post
    Y
    And i basically need:
    saveFile.open(FileName+".lbm"); //instead of having no file type

    Filename is an array of chars,
    This is C++, so dump the array of chars -- use a class that wraps an array of char, i.e. a bonafide string class. Not only are they easier and safer to handle, you don't need to guess the number of characters needed to hold a filename, and you also don't risk buffer overflow.

    Your choices are CString (if using MFC), or if using standard C++, std::string and std::wstring. Since you have ofstream in your code, and I don't know if your app is UNICODE or not, the safest bet is to use std::string, as this is always a non-wide char array, regardless of the build type. The ofstream only knows non-wide characters. If you had a UNICODE build and want to save to a UNICODE file, then you use std::wostream and std::wstring.

    If you were using MFC to open the file instead of standard C++ streams, then you should go with CString, as that will adjust itself to either non-wide or wide string, depending on the build, instead of your code jumping back and forth between std::string and std::wstring, std::ostream, std::wostream. (Yes, you can write macros to choose the string type, but the easiest thing to do, at least in your case, is to just use CString if you are using MFC). So you need to decide whether your files will always be non-UNICODE files, or will vary between non-UNICODE and UNICODE (and of course, you can create UNICODE files in non-UNICODE builds and vice-versa).

    Example of using std::string
    Code:
    #include <string>
    //...
    std::string sFile = "whatever";
    sFile += ".123";
    ofstream ofs(sFile,c_str());
    Note that we need to call c_str() to return a const char* that points to the string itself. This is necessary, since the ofstream() constructor is looking for a const char*, not a std::string. For CString, there is an LPCTSTR casting operator you can use instead of c_str(). Regardless, every string class I know of that has been created for public consumption has some way of returning a pointer to the string data, so that the string class can be used in functions that were created "the old way", i.e. expecting a char array (or char pointer).

    There is no + operator for char arrays, since char arrays are "dumb". Arrays by themselves do not know anything about copying, assignment, operators such as "+" to concatenate, etc. You need to wrap the array in a class, and an array of char (or TCHAR) are wrapped in these string classes I mentioned.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 14th, 2011 at 08:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured