CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Save button MFC

  1. #1
    Join Date
    Jul 2009
    Posts
    21

    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

  2. #2
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    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 !

  3. #3
    Join Date
    Jul 2009
    Posts
    21

    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.

  4. #4
    Join Date
    Jul 2009
    Posts
    21

    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.

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

    Re: Save button MFC

    Your friend is wrong. To append to a file using CFile simply call SeekToEnd() after opening the file for writing.

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

    Re: Save button MFC

    Just looked at your code. Why are you using a CArchive object to write to a text file?

  7. #7
    Join Date
    Jul 2009
    Posts
    21

    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 .

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Save button MFC

    1. create control member variables for all your edit boxes
    2. use CWnd::GetWindowText to read in texts from edit controls to some CString objects
    3. use CStdioFile::WriteString to write these CStrings to the file
    Last edited by VictorN; July 27th, 2009 at 05:20 AM.
    Victor Nijegorodov

  9. #9
    Join Date
    Jul 2009
    Posts
    21

    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

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  11. #11
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    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

  12. #12
    Join Date
    Jul 2009
    Posts
    3

    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

  13. #13
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    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

  14. #14
    Join Date
    Jul 2009
    Posts
    3

    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??

  15. #15
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    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

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
  •  





Click Here to Expand Forum to Full Width

Featured