visual stidio2005中CString和CStdio

来源:百度知道 编辑:UC知道 时间:2024/06/02 12:28:22
小弟学MFC,看的是台湾人写的《深入浅出MFC》2/e

照着书上的示例做实验

#define _AFXDLL

# include <afx.h>
# include <stdio.h>

int main()
{
int lo,hi;
CString str;
CStdioFile fFibo;

fFibo.Open("FIBO.DAT",CFile::modeWrite|CFile::modeCreate|CFile::typeText);

str.Format("%s\n","Fibonacci sequencee,less than 100:");
printf("%s",(LPCTSTR)str);
fFibo.WriteString(str);

lo=hi=1;

str.Format("%d\n",lo);
printf("%s",(LPCTSTR)str);
fFibo.WriteString(str);

while(hi<100)
{
str.Format("%d\n",hi);
printf("%s",(LPCTSTR)str);
fFibo.WriteString(str);
hi=lo+hi;
lo=hi-lo;
}
fFibo.Close();
return 0;
}

错误如下:
z.cpp
1> WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
1>.\z.

晕。。。MFC的兼容性是差一点,你把5.0移植到8.0肯定很多问题啦。。造成不能运行的原因主要是2005增加了一些参数类型的安全性检查。
比如
fFibo.Open("FIBO.DAT",CFile::modeWrite|CFile::modeCreate|CFile::typeText);
它第一个参数类型是LPCTSTR,而你的"FIBO.DAT"只是const char*类型,在文件名"FIBO.DAT"前加上L把它转换就行了。

第二个错误也是。
str.Format("%s\n","Fibonacci sequencee,less than 100:");
在"%s\n"之前加上L。
第三个第四个错误都这样改就OK了……