Saving/loading files from within program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Saving/loading files from within program

  1. #1
    Join Date
    Sep 2007
    Posts
    103

    Saving/loading files from within program

    Hi everyone,

    I would like to write a function for a program of mine that processes all the files in a given directory.
    This would require the ability to save/load files from within the program itself, as well as being able to auto-save every once in a while.
    Is there a way of requesting a save/load in this manner?
    So far the only way I can find of saving/loading is by the user itself in the menu with Ctrl+S and Ctrl+O.

    Thank you in advance.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Saving/loading files from within program

    a function for a program of mine that processes all the files in a given directory.
    For VS2017, this code snippet will process (in this case just display their names) all the regular files in the specified directory.

    Code:
    #include <iostream>
    #include <string>
    #include <string_view>
    using namespace std;
    
    #include <filesystem>
    using namespace std::experimental::filesystem::v1;
    
    void proc_file(string_view fn)
    {
    	cout << fn << endl;
    }
    
    bool proc_dir(const string& dn)
    {
    	if (!exists(dn))
    		return false;
    
    	for (const auto &e : directory_iterator { dn })
    		if (is_regular_file(e.path()))
    			proc_file(e.path().filename().string());
    
    	return true;
    }
    
    int main()
    {
    	const string dir = R"(c:\downloads)"s;
    
    	if (!proc_dir(dir))
    		cout << "Folder " << dir << " does not exist" << endl;
    }
    Last edited by 2kaud; August 27th, 2017 at 09:46 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,954

    Re: Saving/loading files from within program

    Quote Originally Posted by rmirani View Post
    I would like to write a function for a program of mine that processes all the files in a given directory.
    What kind of "processessing" do you mean/consider?

    Quote Originally Posted by rmirani View Post
    This would require the ability to save/load files from within the program itself, as well as being able to auto-save every once in a while.
    Is there a way of requesting a save/load in this manner?
    Yes there is.
    You could just implement a timer and load/save what you need within the OnTimer message handler.
    More preferrable way - do these operations in the worker or UI thread.

    Quote Originally Posted by rmirani View Post
    So far the only way I can find of saving/loading is by the user itself in the menu with Ctrl+S and Ctrl+O.
    Well it is just a one of the lots of ways to implement it.
    Victor Nijegorodov

  4. #4
    Join Date
    Sep 2007
    Posts
    103

    Re: Saving/loading files from within program

    I should probably phrase my question a bit more clearly.
    How can I load a savefile from within my own program (so without the user being the one to open the file himself with Ctrl+O)?
    And how can I save a savefile without the user himself pressing Ctrl+S or going to "File->Save" within the menu?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,954

    Re: Saving/loading files from within program

    You could use CStdioFile class to open/save text files. You can call its methods from anywhere in your program.
    Victor Nijegorodov

  6. #6
    Join Date
    Sep 2007
    Posts
    103

    Re: Saving/loading files from within program

    Quote Originally Posted by VictorN View Post
    You could use CStdioFile class to open/save text files. You can call its methods from anywhere in your program.
    No, I'm referring to regular savefiles; not .txt files.
    The ones that users can create by pressing Ctrl+S or by going to "File->Save".
    The ones that will lead to a call to ::Serialize().

    Is it possible to open those particular savefiles from within the program itself?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,954

    Re: Saving/loading files from within program

    Yes, you can use CFileDialog Class
    Victor Nijegorodov

  8. #8
    Join Date
    Sep 2007
    Posts
    103

    Re: Saving/loading files from within program

    Quote Originally Posted by VictorN View Post
    Yes, you can use CFileDialog Class
    Ok, wonderful.
    Thank you!

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,954

    Re: Saving/loading files from within program

    Quote Originally Posted by rmirani View Post
    Ok, wonderful.
    Thank you!
    You are welcome!
    Victor Nijegorodov

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,504

    Re: Saving/loading files from within program

    CFileDialog doesn't actually open or save files, it just prompts for a name. You need to be more specific about what the files are. If you're using Serialize, then read about the CFile and CArchive classes, and maybe CDocument.

  11. #11
    Join Date
    Jul 2017
    Posts
    14

    Re: Saving/loading files from within program

    The intial snippet from 2kaud seems right, let us know how it works out in the end!

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,504

    Re: Saving/loading files from within program

    Quote Originally Posted by Larnaco View Post
    The intial snippet from 2kaud seems right, let us know how it works out in the end!
    Not for what the OP wants. Read post #6.

  13. #13
    Join Date
    Sep 2007
    Posts
    103

    Re: Saving/loading files from within program

    Quote Originally Posted by GCDEF View Post
    CFileDialog doesn't actually open or save files, it just prompts for a name. You need to be more specific about what the files are. If you're using Serialize, then read about the CFile and CArchive classes, and maybe CDocument.
    Can you be a bit more specific?
    Those are some pretty big classes.
    So far I can't seem to find any way to do this, and I'm getting the impression that it's not possible at all.

    Is it at the very least possible to auto-save a file every once in a while while processing?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,504

    Re: Saving/loading files from within program

    Quote Originally Posted by rmirani View Post
    Can you be a bit more specific?
    Those are some pretty big classes.
    So far I can't seem to find any way to do this, and I'm getting the impression that it's not possible at all.

    Is it at the very least possible to auto-save a file every once in a while while processing?
    It's possible and pretty easy. Is the object you want to serialize derived from CDocument or CObject?

    Just make call the same function the File/Save menu calls, which is typically CDocument::OnSaveDocument.

  15. #15
    Join Date
    Sep 2007
    Posts
    103

    Re: Saving/loading files from within program

    Quote Originally Posted by GCDEF View Post
    It's possible and pretty easy. Is the object you want to serialize derived from CDocument or CObject?

    Just make call the same function the File/Save menu calls, which is typically CDocument::OnSaveDocument.
    It's a CDocument, however, it's ::Serialize() function is of the form:

    void CMyDoc::Serialize(CArchive& ar)

    So it' requires a CArchive object.
    Where would I get this from?

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)