1、我一开始还以为我的fprintf格式写错了呢,排除了一切可能的错误之后,我只能把错误定位到 fStream = fopen("d:\\1.txt","rw+");这句话上。
2、当我把这句话改成 fStream = fopen("d:\\1.txt","w+");时,立马对了,课件文件操作的复杂性,以后一定要好好地注意一下。
正确代码如下:
#include <IOSTREAM.h>
#include <STDIO.H>
#include <PROCESS.H>
void t_printf()
{
}
void main()
{
FILE *fStream = NULL;
char s[] = "this is a string!";
char c = '\n';
fStream = fopen("d:\\1.txt","w+"); //此处是w+,而不是rw+
if (fStream != NULL)
{
fprintf(fStream,"%s%c",s,c);
fclose(fStream);
fStream = NULL;
system("type d:\\1.txt");
}
else
{
cout<<"打开文件失败"<<endl;
}
}