void CEditorDlg::OnOK()
{
m_edit3.GetLine(0,filename); //m_edit3 is the CEdit object that uses the edit box where I get the file name
CString Stringz(filename);
int n = Stringz.Insert(555,".dat");
CFile OutFile(Stringz, CFile::modeCreate | CFile::modeWrite);
char OutS[20];
m_edit1.GetLine(0,OutS); //m_edit1 gets a line of text that is to be stored from an edit box
OutFile.Write(OutS,20);
OutFile.Close();
}
CString
In a prog I'm making in C++ and MFC, I'm trying to get some simple file i/o done, and I'm using CString, but when I try to save it, the file comes up with no name, but the file extension I used is there (right now it's .dat.)
Here's the function thats called when the button is pushed:
It compiles fine and makes a file, but the file has no name but it has the .dat extension.
Hibiki
Wheres the any key?
www.geocities.com/dragongames123/home.html
INSERT MY OVER-SIZED EXTRAVOGENT PIC HERE:
[edited by - HibikiKonzaki on December 15, 2002 10:56:02 PM]
HibikiWheres the any key?www.geocities.com/dragongames123/home.htmlINSERT MY OVER-SIZED EXTRAVOGENT PIC HERE:
Do a printf or debug of filename - make sure there is something in the edit box to get.
The Insert(555) looked strange but http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/vcrefcstringinsert.asp shows the same example so that should be fine.
However when browsing for GetLine Microsoft does some funky code:
#ifdef _DEBUG
// The pointer to my edit.
extern CEdit* pmyEdit;
int i, nLineCount = pmyEdit->GetLineCount();
CString strText, strLine;
// Dump every line of text of the edit control.
for (i=0;i < nLineCount;i++)
{
pmyEdit->GetLine(i, strText.GetBuffer(pmyEdit->LineLength(i)));
strText.ReleaseBuffer();
strLine.Format(TEXT("line %d: ''%s''\r\n"), i, strText.GetBuffer(0));
afxDump << strLine;
}
#endif
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cedit.3a3a.getlinecount.asp or http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cedit.3a3a.getline.asp
Don''t know why they are doing the GetBuffer/ReleaseBuffer ...
The Insert(555) looked strange but http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/vcrefcstringinsert.asp shows the same example so that should be fine.
However when browsing for GetLine Microsoft does some funky code:
#ifdef _DEBUG
// The pointer to my edit.
extern CEdit* pmyEdit;
int i, nLineCount = pmyEdit->GetLineCount();
CString strText, strLine;
// Dump every line of text of the edit control.
for (i=0;i < nLineCount;i++)
{
pmyEdit->GetLine(i, strText.GetBuffer(pmyEdit->LineLength(i)));
strText.ReleaseBuffer();
strLine.Format(TEXT("line %d: ''%s''\r\n"), i, strText.GetBuffer(0));
afxDump << strLine;
}
#endif
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cedit.3a3a.getlinecount.asp or http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cedit.3a3a.getline.asp
Don''t know why they are doing the GetBuffer/ReleaseBuffer ...
Ok, I got it to work. When I changed filename to a local variable, it worked fine.
Also, is there a way to save an int using CFile? I''ve got strings working right, but I can''t find anything on ints. I''m using the docs that came with MSVC++.
Hibiki
Wheres the any key?
www.geocities.com/dragongames123/home.html
INSERT MY OVER-SIZED EXTRAVOGENT PIC HERE:
Also, is there a way to save an int using CFile? I''ve got strings working right, but I can''t find anything on ints. I''m using the docs that came with MSVC++.
Hibiki
Wheres the any key?
www.geocities.com/dragongames123/home.html
INSERT MY OVER-SIZED EXTRAVOGENT PIC HERE:
HibikiWheres the any key?www.geocities.com/dragongames123/home.htmlINSERT MY OVER-SIZED EXTRAVOGENT PIC HERE:
December 16, 2002 10:54 PM
the MS "recommended" way of loading/storing data via a CFile is in conjunction with a CArchive object and the Serialize functions for each loaded/stored CObject-inherited class. the "<<" and ">>" overloaded operators may then be used to load/store any member variables of any given datatype.
...CFile fi("skin.ini", CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);CArchive ar(&fi, CArchive::store);pFileAttributes->Serialize(ar);ar.Close();fi.Close();...
void CFileAttributes::Serialize( CArchive& ar ){ CObject::Serialize(ar); if( ar.IsStoring() ) { // storing code ar << skin; ar << name; ar << type; ar << author; ar << attrib; ar << size; ar << time_access; ar << time_create; ar << time_write; } else { // loading code ar >> skin; ar >> name; ar >> type; ar >> author; ar >> attrib; ar >> size; ar >> time_access; ar >> time_create; ar >> time_write; }}
Also, is there a way to save an int using CFile? I''ve got strings working right, but I can''t find anything on ints.
It writes numbers like int, float, etc. in binary form. And remember to give it a pointer or use & to get a reference. I know its easy to forget that a string is usually represented by a pointer.
int i;
...
Outfile.Write(&i, sizeof(int));
If you are wanting readable text and not binary you''ll need a function to convert from a number to a string, such as itoa.
It writes numbers like int, float, etc. in binary form. And remember to give it a pointer or use & to get a reference. I know its easy to forget that a string is usually represented by a pointer.
int i;
...
Outfile.Write(&i, sizeof(int));
If you are wanting readable text and not binary you''ll need a function to convert from a number to a string, such as itoa.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement