|
-
May 23rd, 2002, 05:15 AM
#1
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_strLogFile, CFile::modeWrite, NULL) )
{
if( !fStream.Open(m_strLogFile, CFile::modeWrite | CFile::modeCreate, NULL) )
{
MessageBox("Open or Create of LogFile faild!\nFile: " + m_strLogFile,"Error");
return;
}
}
fStream.WriteString(strBuffer); :confused:
fStream.Close();
CDialog::OnOK();
}
-
May 23rd, 2002, 05:17 AM
#2
The Syntax is not PHP
I use the PHP Tag for the Syntax-Highligting...
Thats C++ Code.
-
May 23rd, 2002, 05:20 AM
#3
add these flags:
CFile::modeCreate | CFile::modeNoTruncate
to prevent erasing the file.
regards,
MiMec
-
May 23rd, 2002, 05:42 AM
#4
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();
}
-
May 23rd, 2002, 05:46 AM
#5
Try calling SeekToEnd() before writing, to append data to the end of the file.
regards,
MiMec
-
May 23rd, 2002, 05:49 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|