Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Writing integer numbers to a CFile

0 views
Skip to first unread message

Daniel Liu

unread,
Sep 2, 2003, 6:07:22 AM9/2/03
to
Hello,

I need write some integer numbers to a CFile.

Following has some code. The ContactData.txt file has
been created in the same directory as *.cpp file.

int number = 300;

CFile cf;
if( cf.Open( "ContactData.txt",
CFile::modeWrite ) ){

char Data[] = "";
try{
cf.SeekToEnd();
cf.Write( Data, strlen( Data ) );
}

catch( CFileException *e ){
e->Delete();
}
}

Could any body tell me how to make the above code work
properly?

Thanks in advance.

Best wishes,

Daniel


Scott McPhillips

unread,
Sep 2, 2003, 8:04:12 AM9/2/03
to

Is there any data in the char array? As shown above it is empty and
therefore the Write statement will write 0 bytes. The easiest way to do
this is to use CString instead of char[], and use CStdioFile instead of
CFile. (CFile is for binary data, CStdioFile is for text data.) Then
you need to convert the integer into the CString before writing. The
CString Format function can be used for that.

--
Scott McPhillips [VC++ MVP]

ozzy66

unread,
Sep 3, 2003, 8:14:45 AM9/3/03
to
hint: why don't u use CString as char-array?

then u can use CString::Format(itoa(...)) to store the
data and afterwards CString::GetBuffer() to get the
reasonable memory, write to the file and afterwards
CString::ReleaseBuffer() to clear memory.

0 new messages