想在硬盘中存放一些以时间命名的txt文件,放一些数据
代码如下
CString strFileName;
int tempmonth,tempday,temphour,tempmin;
CString tempkind;
tempmonth=tempday=temphour=tempmin=1;
CTime ct=CTime::GetCurrentTime();
tempmonth=ct.GetMonth();
tempday=ct.GetDay();
temphour=ct.GetHour();
tempmin=ct.GetMinute();
tempkind="文件"; strFileName.Format("d\\data\\2001年%02d月%02d日\\%02d时%02d分%s.txt",tempmonth,
tempday,temphour,tempmin,tempkind);
FILE* datafile1;
if((datafile1=fopen(strFileName,"r"))==NULL)//f:\\work\\file
{
AfxMessageBox("打开文件*.txt时出错,请检查!");
//return FALSE ;
}
else
{
float a1=1.2;
fprintf(datafile1,"%f \n",a1);
}老是提示"打开文件*.txt时出错,请检查!",好像创建不了相应的txt文件,怎么创建阿,大侠给电简单的方法撒。
代码如下
CString strFileName;
int tempmonth,tempday,temphour,tempmin;
CString tempkind;
tempmonth=tempday=temphour=tempmin=1;
CTime ct=CTime::GetCurrentTime();
tempmonth=ct.GetMonth();
tempday=ct.GetDay();
temphour=ct.GetHour();
tempmin=ct.GetMinute();
tempkind="文件"; strFileName.Format("d\\data\\2001年%02d月%02d日\\%02d时%02d分%s.txt",tempmonth,
tempday,temphour,tempmin,tempkind);
FILE* datafile1;
if((datafile1=fopen(strFileName,"r"))==NULL)//f:\\work\\file
{
AfxMessageBox("打开文件*.txt时出错,请检查!");
//return FALSE ;
}
else
{
float a1=1.2;
fprintf(datafile1,"%f \n",a1);
}老是提示"打开文件*.txt时出错,请检查!",好像创建不了相应的txt文件,怎么创建阿,大侠给电简单的方法撒。
strFileName.Format("d\\data\\2001年%02d月%02d日
应该是:
strFileName.Format("d:\\data\\2001年%02d月%02d日
你是以读的方式打开的文件,怎么能写入成功呢?关于文本文件的读写,我建议直接使用CStdioFile类,好像更方便一些。
你可以看看该类的用法,下面一点示例: CString strSetFile;
strSetFile = "d:\\data.txt"; CStdioFile file;
if(!file.Open(strSetFile,CFile::modeCreate | CFile::modeWrite))
{
MessageBox("创建文件失败!程序无法正常进行。","严重错误");
return;
} file.WriteString("[DB]\n");
file.WriteString(m_dbPath);
file.WriteString("\n[PATH]\n");
file.WriteString(m_WorkPath);
file.Close();
if(dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName(); std::ofstream outFile;
outFile.open(strFile.LockBuffer());
if(outFile.is_open())
{
CStringArray array;
GetControlData(array);
CString strData;
for(int n=0; n<array.GetSize(); n++)
{
strData = array[n];
strData += "\r\n";
outFile.write(strData.LockBuffer(), strData.GetLength());
strData.UnlockBuffer();
}
}
strFile.UnlockBuffer();
outFile.close();
}给你看一下下面的代吗
要创建新的可以用"w",或"w+",前者是新建并删掉存在的内容。
后者是在原来文件尾添加。
Opens for reading. If the file does not exist or cannot be found, the fopen call fails."w"
Opens an empty file for writing. If the given file exists, its contents are destroyed."a"
Opens for writing at the end of the file (appending) without removing the EOF er before writing new data to the file; creates the file first if it doesn’t exist."r+"
Opens for both reading and writing. (The file must exist.)"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed."a+"
Opens for reading and appending; the appending operation includes the removal of the EOF er before new data is written to the file and the EOF er is restored after writing is complete; creates the file first if it doesn’t exist.
if((datafile1=fopen(strFileName,"r"))==NULL)//f:\\work\\file
不应该是只读方式,应改为
if((datafile1=fopen(strFileName,"w"))==NULL)//f:\\work\\file
if((datafile1=fopen(strFileName,"w+"))==NULL)//f:\\work\\file
如果要在文件尾部追加,则要用
if((datafile1=fopen(strFileName,"a"))==NULL)//f:\\work\\file
if((datafile1=fopen(strFileName,"r"))==NULL)//f:\\work\\file
不应该是只读方式,应改为
if((datafile1=fopen(strFileName,"w"))==NULL)//f:\\work\\file
if((datafile1=fopen(strFileName,"w+"))==NULL)//f:\\work\\file
如果要在文件尾部追加,则要用
if((datafile1=fopen(strFileName,"a"))==NULL)//f:\\work\\file
if((datafile1=fopen(strFileName,"r"))==NULL)//f:\\work\\file
不应该是只读方式,应改为
if((datafile1=fopen(strFileName,"w"))==NULL)//f:\\work\\file
if((datafile1=fopen(strFileName,"w+"))==NULL)//f:\\work\\file
如果要在文件尾部追加,则要用
if((datafile1=fopen(strFileName,"a"))==NULL)//f:\\work\\file
用fopen(strFileName,"w")撒。
===============
CString strCmd;
strCmd.Format("type nul>%s", strFileName);system(strCmd);
===============
只要strFileName没有错就一定能建立文件.