简体   繁体   English

MFC C++:如何将某个文件实际保存到系统,而不仅仅是打开另存为对话框

[英]MFC C++: How to actually save a certain file to the system, not just open a save as dialog box

I searched everywhere but I can't find sample on how to actually save a file to the system.我到处搜索,但找不到有关如何将文件实际保存到系统的示例。 Threads about opening a Save File dialog box can be read in numerous sites but the successful saving of the user created file to a user selected path is always cut (//add your code here).可以在许多站点中阅读有关打开“保存文件”对话框的主题,但是始终将用户创建的文件成功保存到用户选择的路径中(//在此处添加您的代码)。 Please bear with me as I am new in C++ (MFC).请耐心等待,因为我是 C++ (MFC) 的新手。

I know I need to actually code the saving of the data to the file path but I just don't know how.我知道我需要实际编码将数据保存到文件路径,但我只是不知道如何。

Code snippet (via CFileDialog):代码片段(通过 CFileDialog):

void CTryDlg::OnBnClickedSaveAs()
{
    CFileDialog dlg(FALSE);
    dlg.m_ofn.nMaxFile = MAX_PATH;
    dlg.m_ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0");
    dlg.m_ofn.lpstrTitle = _T("Save File As");
    CString filename;
    if (dlg.DoModal() == IDOK)
    {
        filename = dlg.GetPathName(); // return full path and filename
        //write your sample code here to save the file to the user selected path
    }                                                                                           
}

Code snippet via GetSaveFileName():通过 GetSaveFileName() 的代码片段:

OPENFILENAME SfnInit()
{
    OPENFILENAME t_sfn;

    char szFileName[MAX_PATH] = "";
    ZeroMemory(&t_sfn, sizeof(t_sfn));
    t_sfn.lStructSize = sizeof(t_sfn);
    t_sfn.hwndOwner = NULL;
    t_sfn.lpstrFilter = _T("Text file\0*.txt\0");
    t_sfn.lpstrFile = szFileName;
    t_sfn.lpstrTitle = _T("Save As\0");
    t_sfn.nMaxFile = MAX_PATH;
    t_sfn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    t_sfn.lpstrDefExt = _T("Text file\0*.txt\0");

    if (GetSaveFileName(&t_sfn2) != true)
    {
        AfxMessageBox(_T("Saving file canceled!"));
    }
    else
    {
        //write your sample code here to save the file to the user selected path
    }
}

Anybody who can provide a very simple sample code that could actually save a user desired file (ex: text file) to the user selected path will be greatly appreciated.任何可以提供非常简单的示例代码的人,该代码实际上可以将用户所需的文件(例如:文本文件)保存到用户选择的路径,将不胜感激。 I have also read that the program should run as administrator.我还读到该程序应该以管理员身份运行。

Thank you.谢谢你。

Since you are using MFC, I would recommend sticking with MFC classes for such file I/O.由于您使用的是 MFC,因此我建议您对此类文件 I/O 坚持使用 MFC 类。

Sadly, I am using VS 2008, but here is the class hierarchy for CFile:可悲的是,我使用的是 VS 2008,但这里是 CFile 的 class 层次结构:

If it's a text file, using/deriving from CStdioFile makes sense.如果它是一个文本文件,使用/派生自 CStdioFile 是有意义的。 It has the basic ReadString WriteString methods.它具有基本的 ReadString WriteString 方法。

However, if you are wanting to serialize something derived from CDocument (Document/View architecture), you will want to utilize streams, possibly with schemas/versioning to go with your serialization.但是,如果您想要序列化从 CDocument(文档/视图架构)派生的内容,您将需要使用流,可能通过序列化将模式/版本控制为 go。 That's a completely different topic/answer.这是一个完全不同的主题/答案。

EDIT: duh - here's a simple CStdioFile output编辑:duh - 这是一个简单的 CStdioFile output

        CFileDialog fd(FALSE, "txt", "MyFile.txt", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text files (*.txt)|*.txt|All files (*.*)|*.*", this);
        if (fd.DoModal() == IDOK)
            {
            CStdioFile fOut(fd.GetPathName(), CFile::modeCreate | CFile::modeWrite);
            for (int i = 0; i < asData.GetSize(); i++)
                {
                fOut.WriteString(asData[i] + '\n');
                }
            fOut.Close();
            }

Here is a very basic sample:这是一个非常基本的示例:

...
if (dlg.DoModal() == IDOK)
{
    filename = dlg.GetPathName(); // return full path and filename

    FILE *file = fopen(filename, "w");    // open file for writing

    if (file == NULL)
      AfxMessageBox("File couild not be created."};
    else
    {
      // file could be created, write something
      fprintf(file, "Some text\n");

      // and close the file
      fclose(file);
    }
}
...

This will write "some text" into the file whose name has been provided by the user with the CFileDialog file picker.这会将“一些文本”写入用户使用 CFileDialog 文件选择器提供名称的文件中。

In real world you need to write whatever text according to the data of your program.在现实世界中,您需要根据程序的数据编写任何文本。

This is really most basic knowledge.这确实是最基本的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM