-
Save button MFC
hi guys nid some help here
For the save button i was suppose to save my input into a text file.And i was suggested by my friends to use append so that when i typed in a new input the old input wouldnt get deleted,so hw shall i go about doing this.
Before that i was using CFile but my friend said that CFile doesnt have an append function so my old input would get deleted.
THis is my old code for reference.
void CFileProcDlg::OnBnClickedSaveBtn()
{
// TODO: Add your control notification handler code here
this->UpdateData();
CFile f;
char strFilter[] = { "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||" };
CFileDialog FileDlg(FALSE, ".txt", NULL, 0, strFilter);
if( FileDlg.DoModal() == IDOK )
{
f.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
CArchive ar(&f, CArchive::store);
ar << m_Date << m_Time << m_File ;
ar.Close();
}
else
return;
f.Close();
ps. im using MFC vs 2005
-
Re: Save button MFC
To achieve your goal, try
Code:
#include <fstream.h> //Needed to create output file
void main()
{
ofstream File; //Names File as ofstream (for output to file)
File.open("File.txt",ios::out); //Opens "File.txt" for output
File << "This is the first line.\n"; //Outputs to file
File << "This is line two."; //Outputs to file
File << " This is still on line two.\n";//Outputs to file
File.close(); //Closes file
File.open("File.txt",ios::app); //Reopens file to append, if you just used ios::out again, it would erase everything and rewrite the file
File << "This is appending the text file. This is line 3.\n"; //Outputs to file
File << "And finally line 4!\n"; //Outputs last line of file
File.close(); //Closes opened file
}
-
Re: Save button MFC
hi ,sorry i am noob..where am i suppose to type tis code at?and i use edit boxes to type the data into,so how shld i let it allow to take the data from the edit box and store into the text file and also append.
-
Re: Save button MFC
sorry but just want to ask does the code works in microsoft foundation class 2005? my friend suggest using the fopen fuction as it has append fuction.
-
Re: Save button MFC
Your friend is wrong. To append to a file using CFile simply call SeekToEnd() after opening the file for writing.
-
Re: Save button MFC
Just looked at your code. Why are you using a CArchive object to write to a text file?
-
Re: Save button MFC
so where am i suppose to include the line in my program.
and im suppose to key in data into the 3 edit boxes,den save them into a text file,so how do i
add the edit boxes into the code?
sorry for asking so much and thanks for all that help .
-
Re: Save button MFC
- create control member variables for all your edit boxes
- use CWnd::GetWindowText to read in texts from edit controls to some CString objects
- use CStdioFile::WriteString to write these CStrings to the file
-
Re: Save button MFC
hi may i know hw to code the 2nd one if i have m_Date and m_Time which i had set the control on
-
Re: Save button MFC
If m_Date and m_Time are the control member variables (of the type CEdit or any CEdit derived class) then:
Code:
CString strTextDate;
m_Date.GetWindowText(strTextDate);
CString strTextTime;
m_Time.GetWindowText(strTextTime);
-
Re: Save button MFC
Another thing:
If you want to open the file to append text without deleting the content, you need to use the CFile::modeNoTruncate flag:
Code:
CStdioFile file;
file.Open(filename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
...
-
Re: Save button MFC
Hi folks,
Sorry to interrupt,Can i post my question here as i had the same problem for
point 3 (use CStdioFile::WriteString to write these CStrings to the file ).
Do anyone have any example on it?
Thanks ,
nancy
-
Re: Save button MFC
Code:
CString str = _T("String to write");
file.WriteString(str);
Note that WriteString does not add the end of line characters, so you would nee to do it yourself when you need it:
Code:
CString str = _T("String to write\r\n");
file.WriteString(str);
-
Re: Save button MFC
Hi,
Thanks for ur prompt reply elrond,just want to ask ("String to write\r\n");--> what is this line for??
-
Re: Save button MFC
An example for the end of line characters "\r\n"
If you don't use them, when you open the file with a text editor, everything will be on the same line. If that suits you, that's fine not to use them. But when writing a text file, in most cases the text will be on multiple lines/paragraphs, so you will need to be able to indicate the end of line and beginning of a new line. It is what these characters are for.