How to creat a new folder and a new file
-
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();
-
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();
zhangguoliming wrote:
strcat(path,m_Luting);
This should probably be
strcpy()
instead.zhangguoliming wrote:
fMyfile.open(path,ios:out|ios::binary|ios::trunc);
What is the value of
path
at this point? On a side note, since you are using MFC, have you looked intoCFile
?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();
- don't put
m_Luting
in a_T()
macro. 2) Since you're using MFC, use it all the way...
if (CreateDirectory(m_Luting))
{
CString sPath;
sPath.Format("%s\\%s%s", m_Luting, m_Name, stName);
CFile file;
CFileException e;
if (file.Open(sPath, CFile::modeCreate | CFile::modeWrite, &e)
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
return;
}
else
{
file.Close();
}
}
else
{
AfxMessageBox("Could not create folder.");
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 - don't put
-
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();
zhangguoliming wrote:
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance.
-
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();
You can also use of
CreateFile
for create or open a file
WhiteSky