| | |
Saving a file using C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 7
Hi Everyone,
I need some help in saving a file in the rich text edit control i have. Basically i have one button and richedit control on a form and what i need is the contents of the entire rich edit control to be passed to the saveas common dialog to be saved onto thee disk. One more thing to note is that my rich edit also contains pictures and text. Can anyone tell me how this is done or show me the codings of how this is done. So far this all that i have gotten so far.
Void CDialogsDlg::onSaveAs ()
{
CFileDialog m_ldFile(false);
if(m_ldFile.DoModal = = IDOK)
{
//This is the part i do not know how to save the entire contents
// of the rich edit
}
}
Someone please help me . My e-mail is freesoft_2000@yahoo.com
Thank You
Yours Sincerely
Richard West
I need some help in saving a file in the rich text edit control i have. Basically i have one button and richedit control on a form and what i need is the contents of the entire rich edit control to be passed to the saveas common dialog to be saved onto thee disk. One more thing to note is that my rich edit also contains pictures and text. Can anyone tell me how this is done or show me the codings of how this is done. So far this all that i have gotten so far.
Void CDialogsDlg::onSaveAs ()
{
CFileDialog m_ldFile(false);
if(m_ldFile.DoModal = = IDOK)
{
//This is the part i do not know how to save the entire contents
// of the rich edit
}
}
Someone please help me . My e-mail is freesoft_2000@yahoo.com
Thank You
Yours Sincerely
Richard West
Introduction
---------------
The Visual Component Library (VCL) provides various built-in classes to perform file processing. Most of the features are provided through the TFileStream class. To perform file streaming using this class, first declare its instance using its constructor whose syntax is:
The first argument of this constructor is the name (or path) of the file you are dealing with. If the file does not exist or it cannot be accessed (opened or saved) for any reason, the compiler would throw an error and stop the action.
The second action, Mode, specifies what you are trying to do with the file.
The TFileStream class is conceptually designed to deal with the contents of one or more controls. Therefore, instead of concerning yourself with the values of controls, TFileStream would consider the change that affect a control on behalf of the user, which mostly is its contents. When saving a file, TFileStream faithfully gets the contents of all controls as you wish and saves them in one file. If opening a file, TFileStream locates the content of each file and restores it. Based on this, TFileStream is appropriate for VCL objects and usually its files should not mixed with non-VCL controls.
Saving Controls Contents
In order to save the contents of controls, first declare a pointer to TFileStream using its constructor and specify where the file would be saved. You can specify this path directly in the constructor if you know exactly where the file should be located. This can be done if you are writing a program that stores the default file at a specific location and you know where the file should be saved. Otherwise, you can use the SaveDialog control to let the user specify where to save the file.
When saving a file, the Mode argument of the constructor can be passed as one of the following constants:
Write Mode Description
fmCreate If the user is saving a new file, this is used to save it as new
fmOpenWrite This is used to save a file as new. If the file was previously saved and reopened, this mode would erase its previous contents and fill it with the new data
fmOpenReadWrite If the file already existed, this can be used to save a file that has been modified. Otherwise it can be used to create a new file
When it is possible that other people or application would try accessing the same file at the same time, the following constants can be used to manage the sharing of files:
Share Mode Description
fmShareExclusive Only the current application can access the file
fmShareDenyWrite The file can be opened by other applications but they cannot modify its contents
fmShareDenyRead The file can be modified by other applications but they cannot open it
fmShareDenyNone There is no restriction on what the other applications can do with the current file
To combine these two mode for the Mode argument, you use the bitwise OR operator |
Imagine you create a form equipped with a Memo and an Edit controls:
Here is an example of saving the contents of both controls to a file:
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
Loading Controls Contents
When saving the contents of controls using TFileStream, the file is arranged so the class can locate data for each object. Based on this, you can use TFileStream to open a file that was created with this class. To do this, once again, declare a pointer to TFileStream and initialize the file using the constructor. If you already know where the file is located, you can simply provide it to the constructor. Otherwise, you can use the Open dialog box and let the user select the file.
When opening a file, you can use one of the following modes as the Mode argument:
Read Mode Description
fmOpenRead This is used to open a file but the user cannot modify and then save it.
fmOpenReadWrite This allows opening an existing file, modifying, and saving it.
You can combine this mode with one of the above share modes using the bitwise OR operator. Here is an example from the same above form design:
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
---------------
The Visual Component Library (VCL) provides various built-in classes to perform file processing. Most of the features are provided through the TFileStream class. To perform file streaming using this class, first declare its instance using its constructor whose syntax is:
C++ Syntax (Toggle Plain Text)
__fastcall TFileStream(const AnsiString FileName, Word Mode);
The second action, Mode, specifies what you are trying to do with the file.
The TFileStream class is conceptually designed to deal with the contents of one or more controls. Therefore, instead of concerning yourself with the values of controls, TFileStream would consider the change that affect a control on behalf of the user, which mostly is its contents. When saving a file, TFileStream faithfully gets the contents of all controls as you wish and saves them in one file. If opening a file, TFileStream locates the content of each file and restores it. Based on this, TFileStream is appropriate for VCL objects and usually its files should not mixed with non-VCL controls.
Saving Controls Contents
In order to save the contents of controls, first declare a pointer to TFileStream using its constructor and specify where the file would be saved. You can specify this path directly in the constructor if you know exactly where the file should be located. This can be done if you are writing a program that stores the default file at a specific location and you know where the file should be saved. Otherwise, you can use the SaveDialog control to let the user specify where to save the file.
When saving a file, the Mode argument of the constructor can be passed as one of the following constants:
Write Mode Description
fmCreate If the user is saving a new file, this is used to save it as new
fmOpenWrite This is used to save a file as new. If the file was previously saved and reopened, this mode would erase its previous contents and fill it with the new data
fmOpenReadWrite If the file already existed, this can be used to save a file that has been modified. Otherwise it can be used to create a new file
When it is possible that other people or application would try accessing the same file at the same time, the following constants can be used to manage the sharing of files:
Share Mode Description
fmShareExclusive Only the current application can access the file
fmShareDenyWrite The file can be opened by other applications but they cannot modify its contents
fmShareDenyRead The file can be modified by other applications but they cannot open it
fmShareDenyNone There is no restriction on what the other applications can do with the current file
To combine these two mode for the Mode argument, you use the bitwise OR operator |
Imagine you create a form equipped with a Memo and an Edit controls:
Here is an example of saving the contents of both controls to a file:
//---------------------------------------------------------------------------
C++ Syntax (Toggle Plain Text)
void __fastcall TForm1::btnSaveClick(TObject *Sender) { // Decalre a pointer to TFileStream TFileStream *FStream; // Let the user call the Save Dialog if( SaveDialog1->Execute() ) { // Use the constructor of the TFileStream to create a file try { FStream = new TFileStream(SaveDialog1->FileName, fmCreate); // In the pointer to FStream, add the contents of the Memo FStream->WriteComponent(Memo1); // and the content of the Edit controls FStream->WriteComponent(Edit1); } __finally { // Since the pointer was created, delete it, // whether it was used or not delete FStream; } } }
Loading Controls Contents
When saving the contents of controls using TFileStream, the file is arranged so the class can locate data for each object. Based on this, you can use TFileStream to open a file that was created with this class. To do this, once again, declare a pointer to TFileStream and initialize the file using the constructor. If you already know where the file is located, you can simply provide it to the constructor. Otherwise, you can use the Open dialog box and let the user select the file.
When opening a file, you can use one of the following modes as the Mode argument:
Read Mode Description
fmOpenRead This is used to open a file but the user cannot modify and then save it.
fmOpenReadWrite This allows opening an existing file, modifying, and saving it.
You can combine this mode with one of the above share modes using the bitwise OR operator. Here is an example from the same above form design:
//---------------------------------------------------------------------------
C++ Syntax (Toggle Plain Text)
void __fastcall TForm1::btnOpenClick(TObject *Sender) { TFileStream *FStream; if( OpenDialog1->Execute() ) { try { FStream = new TFileStream(OpenDialog1->FileName, fmOpenRead | fmShareExclusive); FStream->ReadComponent(Memo1); FStream->ReadComponent(Edit1); } __finally { delete FStream; } } }
Real Eyes Realize Real Lies
My Resume
My Resume
Everyone is familar is the Common Dialog - File Open/Save control in Windows, you use it everyday. This script shows you how to script the Common Dialog control and put it to work for you.
--------------------------------------------------------------------------------------
Note: This script will run on system's with the Microsoft Controls properly installed. If your machine has virtually any of Microsoft's development tools installed, you probably have these controls. I have yet to find an easy way to distribute these, I am open to suggestions.
--------------------------------------------------------------------------------------
Note: This script will run on system's with the Microsoft Controls properly installed. If your machine has virtually any of Microsoft's development tools installed, you probably have these controls. I have yet to find an easy way to distribute these, I am open to suggestions.
C++ Syntax (Toggle Plain Text)
// Create the Objectcd = new ActiveXObject("MSComDlg.CommonDialog"); // Set file filtercd.Filter = "All Files(*.*)|*.*|JScript Files(*.js)|*.js";cd.FilterIndex = 2; // Must set MaxFileSize. Otherwise you will get an errorcd.MaxFileSize = 128; // Show it to usercd.ShowOpen(); // Retrieve file + pathfile = cd.FileName; // If the user does enter file exitif ( !file ) { WScript.Echo("You must enter a file name"); WScript.Quit(0); } else { WScript.Echo("The user selected:\n" + file ); }
Real Eyes Realize Real Lies
My Resume
My Resume
![]() |
Similar Threads
- saving a file to database (C#)
- Saving excel sheet with few column names present as file name (Visual Basic 4 / 5 / 6)
- Photoshop file saving problem (Graphics and Multimedia)
- Photoshop file saving problem (Windows Software)
- Saving input data to a .txt file (Python)
- While I am Downloading ,One Dialogbox will open.When user will be saving file name... (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: don't understand
- Next Thread: Is ifs is a member function of ifstream class
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





