CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2002
    Posts
    147

    Question Why does CFile::modeWrite reset my file content?

    Hi

    I can't figure out what the problem is.
    In my last post some users suggested me to use the CStdioFile for reading a whole line. That works FINE! I used the same Object to Write a file with the method WriteString. That works but the problem is i'd like to append Text in a file. However when i open the file with CFile::modeWrite and append data with WriteString the content of the file will be reseted to 0.

    It is documented that when i use "CFile::modeWrite | CFile::modeCreate" the file will be reseted but only the CFile::modeWrite should not reset the file to 0?

    What's my failure?

    Thank you.
    jaz



    PHP Code:
    void CLogAppWndDlg::OnOK()
    {
        
    // TODO: Add extra validation here
        
    CStdioFile fStream;
        
    CString strBuffer;

        
    UpdateData(TRUE);

        
    strBuffer m_tDatePicker.Format("%Y.%m.%d\t%a\t%H:%M:%S");
        
    strBuffer strBuffer "\t" m_strTxt "\t" m_strComment "\n";

        if( !
    fStream.Open(m_strLogFileCFile::modeWriteNULL) )
        {
            if( !
    fStream.Open(m_strLogFileCFile::modeWrite CFile::modeCreateNULL) )
            {
                
    MessageBox("Open or Create of LogFile faild!\nFile: " m_strLogFile,"Error");
                return;
            }
        }

        
    fStream.WriteString(strBuffer);  :confused
        
    fStream.Close();

        
    CDialog::OnOK();


  2. #2
    Join Date
    Apr 2002
    Posts
    147
    The Syntax is not PHP
    I use the PHP Tag for the Syntax-Highligting...
    Thats C++ Code.

  3. #3
    Join Date
    May 2002
    Location
    Poland
    Posts
    48
    add these flags:
    CFile::modeCreate | CFile::modeNoTruncate
    to prevent erasing the file.
    regards,
    MiMec

  4. #4
    Join Date
    Apr 2002
    Posts
    147
    Thank you for answering but that has not the effect i'm looking for... The code on the bottom creates or opens the file for writing with NoTruncate like you said.

    However this code does not append code. It resets the file and writes the data.

    Is it the WriteString which truncates my file content?

    Thanx for helping
    jaz


    Code:
    void CLogAppWndDlg::OnOK()
    {
    	// TODO: Add extra validation here
    	CStdioFile fStream;
    	CString strBuffer;
    
    	UpdateData(TRUE);
    
    	strBuffer = m_tDatePicker.Format("%Y.%m.%d\t%a\t%H:%M:%S");
    	strBuffer = strBuffer + "\t" + m_strTxt + "\t" + m_strComment + "\n";
    
    	if( !fStream.Open(m_strLogFile, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, NULL) )  :confused: :confused: :confused: 
    	{
    		MessageBox("Open or Create of LogFile faild!\nFile: " + m_strLogFile,"Error");
    		return;
    	}
    	fStream.WriteString(strBuffer); :confused: :confused: :confused: 
    	fStream.Close();
    
    	CDialog::OnOK();
    }

  5. #5
    Join Date
    May 2002
    Location
    Poland
    Posts
    48
    Try calling SeekToEnd() before writing, to append data to the end of the file.
    regards,
    MiMec

  6. #6
    Join Date
    Apr 2002
    Posts
    147
    Jooooo! Perfect, that was the problem!
    Thank you MiMec

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