-
July 21st, 2009, 12:07 AM
#1
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
-
July 21st, 2009, 12:38 AM
#2
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
}
Little by little one goes far
Keep moving.......!
Nothing is impossible !
-
July 21st, 2009, 02:15 AM
#3
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.
-
July 21st, 2009, 02:47 AM
#4
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.
Last edited by kacaka; July 21st, 2009 at 02:50 AM.
-
July 21st, 2009, 07:15 AM
#5
Re: Save button MFC
Your friend is wrong. To append to a file using CFile simply call SeekToEnd() after opening the file for writing.
-
July 21st, 2009, 07:17 AM
#6
Re: Save button MFC
Just looked at your code. Why are you using a CArchive object to write to a text file?
-
July 22nd, 2009, 02:21 AM
#7
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 .
-
July 22nd, 2009, 02:32 AM
#8
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
Last edited by VictorN; July 27th, 2009 at 05:20 AM.
Victor Nijegorodov
-
July 26th, 2009, 10:36 PM
#9
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
-
July 27th, 2009, 05:19 AM
#10
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);
Victor Nijegorodov
-
July 27th, 2009, 08:43 AM
#11
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);
...
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
July 28th, 2009, 01:49 AM
#12
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
-
July 28th, 2009, 02:14 AM
#13
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);
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
July 28th, 2009, 02:22 AM
#14
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??
-
July 28th, 2009, 02:26 AM
#15
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.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|