CStdioFile写文件中出现的问题

来源:互联网 发布:吉林师范大学网络 编辑:程序博客网 时间:2024/05/29 18:49

被一个bug纠缠了两天,今天总算找到问题在哪里了:

 

//创建学生端存放考试结果文件夹
 CString strFolderPath = "C://";
 strFolderPath += _T("学生文件夹");
 if (!CreateDirectory(strFolderPath, NULL))
 {
  AfxMessageBox("创建文件夹失败");
  return;
 }
 
 CString csRscFile = strFolderPath + "//";
 csRscFile += _T("学生一.txt");
 
 if (!stuFile.Open(csRscFile,CStdioFile::modeCreate|CStdioFile::modeWrite|CStdioFile::typeText))
 {
  AfxMessageBox("打开失败");
  return;
 }

 int m_progressPos = 20;
 CString str;
 str.Format("%d",m_progressPos);
 stuFile.WriteString("学生分数:"+str+"/n");

 stuFile.Close();

 

两个应该注意的地方:

1、int转换CString类型 :str.Format("%d",m_progressPos); 起初一直都以为是str.Format("%s",m_progressPos); 这个问题放了两天一直没有发现,唉唉,以后在用函数之前一定要先看清楚用法!泪奔...

    扩展:

    CString转换int类型:CString   str; atoi(str); 

2、CStdioFile写文件之后一定要使用Close函数关闭该文件,否则写文件之后,程序崩溃。

原创粉丝点击