想在硬盘中存放一些以时间命名的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文件,怎么创建阿,大侠给电简单的方法撒。

解决方案 »

  1.   

    老大,你的代码 写错了一个小地方:
    strFileName.Format("d\\data\\2001年%02d月%02d日
    应该是:
    strFileName.Format("d:\\data\\2001年%02d月%02d日
      

  2.   

    好像这里有问题吧:    fopen(strFileName,"r")
        你是以读的方式打开的文件,怎么能写入成功呢?关于文本文件的读写,我建议直接使用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();
      

  3.   

    楼主你可以用CStdioFile sFile;
      

  4.   

    CFileDialog dlg(FALSE);
    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();
    }给你看一下下面的代吗
      

  5.   

    Layworld(Layworld) :   以只读的方式打开,当然是不可以写的。但是,楼主的问题远在写之前就已经发生了呀!!
      

  6.   

    晕,这里都是捣糨糊的啊,"r"方式只能打开存在的文件,
    要创建新的可以用"w",或"w+",前者是新建并删掉存在的内容。
    后者是在原来文件尾添加。
      

  7.   

    同意 sirius12() ,因该用“w”或"w+"
      

  8.   

    呵呵,大家大概都是一时糊涂了吧,看一看MSDN里的原话"r"
    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.
      

  9.   

    此句错拉,
    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
      

  10.   

    此句错拉,
    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
      

  11.   

    此句错拉,
    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
      

  12.   

    晕死
    用fopen(strFileName,"w")撒。
      

  13.   

    来晚了。按Layworld(Layworld说的做,用CStdioFile方便点。
      

  14.   

    用fopen(strFileName,"w")你是用读模式打开的,磁盘上又没有这个文件,当然会报错了。
      

  15.   

    最最方便的方法:
    ===============
    CString strCmd;
    strCmd.Format("type nul>%s", strFileName);system(strCmd);
    ===============
    只要strFileName没有错就一定能建立文件.