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

Thread: Save button MFC

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

    Re: Save button MFC

    Quote Originally Posted by Elrond View Post
    ... 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);
    This code is not 100% correct.
    you should NOT add CR ('\r') character yourself, only LF ('\n'). CStdioFile::WriteString will then replace
    ... any newline character ... written to the file as a carriage return–linefeed pair.
    So, the correct code is:
    Code:
    CString str = _T("String to write\n");
    file.WriteString(str);
    Victor Nijegorodov

  2. #17
    Join Date
    Jul 2009
    Posts
    3

    Re: Save button MFC

    Hi,

    Do i still need mode create if i am just saving all the data into a file i already created.Also which is the line that opens up the directory so that u can save it to your file?Do i have to include seektoend() below
    file.Open(filename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);


    Thanks
    nancy

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

    Re: Save button MFC

    Quote Originally Posted by nancy87 View Post
    ... Also which is the line that opens up the directory so that u can save it to your file?
    You don't need to "open a directory".
    Just use CFileDialog::GetPathName to obtain the full path name of a file and then pass this full path name to CStdIo::Open

    Quote Originally Posted by nancy87 View Post
    ... Do i have to include seektoend() below
    file.Open(filename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
    Yes, if you want to append new data to the old file.
    Victor Nijegorodov

  4. #19
    Join Date
    Jul 2009
    Posts
    21

    Re: Save button MFC

    hi guys
    may i know which part i went wrong my code is here

    void CabcdeDlg::OnBnClickedButton1()
    {
    CString strTextTime;
    m_Time.GetWindowText(strTextTime);

    CStdioFile File;

    file.Open(lpszfilename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
    file.SeekToEnd();

    file.Write(" hello world");

    CString str = _T("String to write\n");
    file.WriteString(str);

    file.Close();

    }
    Last edited by kacaka; August 3rd, 2009 at 08:18 PM.

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

    Re: Save button MFC

    Do not forget to close file when writting string into a file.
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

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

    Re: Save button MFC

    Quote Originally Posted by kacaka View Post
    hi guys
    may i know which part i went wrong my code is here
    ...
    What is the problem?

    Besides:
    1. The variable filename is not defined.
    2. You have to use CFileException to know whether your file operations succeded or not. See the code examples in MSDN articles CFile::Open and CStdioFile::CStdioFile
    Victor Nijegorodov

  7. #22
    Join Date
    Jul 2009
    Posts
    21

    Re: Save button MFC

    Hi,victorN
    Understand that you do not want to spoonfeed me with giving me the code,but after trying quite long i still cant compile and my code shows lots of arrow care to rpovide more help??sorry i started this nt long ago.

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

    Re: Save button MFC

    Quote Originally Posted by kacaka View Post
    ... after trying quite long i still cant compile and my code shows lots of arrow care to rpovide more help??
    But you haven't shown your actual code yet! Nor showed you the exact compile errors!

    So how could we help you more?
    Victor Nijegorodov

  9. #24
    Join Date
    Jul 2009
    Posts
    21

    Re: Save button MFC

    hi,
    my code as above, may i ask why there are error c2228 like left of 'seekToEnd' must have class /struct union. the same error appears for 'write' n 'close','writestring',error c2065 lpszfileName undeclared identifier'writestring'.

    The variable filename is not defined. ?? may i know hw to define tis.

    for point 2
    You have to use CFileException to know whether your file operations succeded or not. See the code examples in MSDN articles CFile::Open and CStdioFile::CStdioFile

    CFileException Exception;
    CFile File;

    if(File.Open("c:\\data.txt", CFile::modeRead, &Exception) == FALSE)
    {



    may i know whether the code is correct .

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Save button MFC

    Quote Originally Posted by kacaka View Post
    hi,
    my code as above, may i ask why there are error c2228 like left of 'seekToEnd' must have class /struct union. the same error appears for 'write' n 'close','writestring',error c2065 lpszfileName undeclared identifier'writestring'.

    The variable filename is not defined. ?? may i know hw to define tis.

    for point 2
    You have to use CFileException to know whether your file operations succeded or not. See the code examples in MSDN articles CFile::Open and CStdioFile::CStdioFile

    CFileException Exception;
    CFile File;

    if(File.Open("c:\\data.txt", CFile::modeRead, &Exception) == FALSE)
    {



    may i know whether the code is correct .
    You didn't show any of the code that generated those errors. Keep in mind though that C++ is case sensitive.

  11. #26
    Join Date
    Jul 2009
    Posts
    21

    Re: Save button MFC

    hi ,
    i have solved the errors now and it works.any question i will post on a new thread thank you everyone

Page 2 of 2 FirstFirst 12

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