写文件的头文件

来源:互联网 发布:pr视频剪辑软件下载 编辑:程序博客网 时间:2024/04/18 08:26
#include <iostream>

#include <fstream>


ofstream f1("D:\\Projects\\Chris_lu\\Demo\\x64\\Debug\\try.txt",ios::trunc);
 if(!f1)
        {
cout << "open file error." << endl;
       // return 0;
   }
 f1.write(strFormula.c_str(),strFormula.length());
 f1.close(); 



为了打开一个文件供输入或输出,除了iostream头文件外,还必须包含头文件:
  #include <fstream>

cannot convert from 'const char' to 'LPCTSTR'
2010-09-11 22:39
    小学期的计算机实习期间,用老师给的一个输出字符的头文件来做俄罗斯方块。为了调试方便就用上了VS2008,但是悲剧的事情随之而来,一开始的时候编译上海没有什么问题,在加了所给的头文件之后,就一直出现 cannot convert parameter 6 from 'const char [3]' to 'LPTSTR'  的错误。起初以为是我的VS2008的设置问题,一狠心就卸了2008装上了最新的2010.本以为问题就此解决,没想到一个来小时的折腾,换来的是一样的结果,无奈之下,只好用DEV来开发了。(因为2008的缘故,VC6在安装过程中一直会报错)
今天找到原因,造成不能运行的原因主要在于2008和2010中增加了一些参数类型的安全性检查,所以通常在6.0没有问题的LPCTSTR与 const char之间的转换到了2008中就行不通了。
于是微软给出的解决办法有两个:
  1. Change your project configuration to use multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set".
  2. Indicate that the string literal, in this case "Hello world!" is of a specific encoding. This can be done through either prefixing it with L, such as L"Hello world!", or surrounding it with the generic _T("Hello world!") macro. The latter will expand to the L prefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise.


 CStdioFile   file;
// CFileException mExcept;
file.Open(L"E:\\1.txt",CFile::modeCreate|CFile::modeWrite);
 if(!file)
        {
cout << "open file error." << endl;
       // return 0;
   }
    file.WriteString (strItemName);
  file.WriteString(L"\n");
 file.Close(); 

原创粉丝点击