c++输出流问题
本帖最后由 xhwjc2008 于 2012-12-17 17:05:17 编辑 我最后的p输出不到控制台窗口中,或者是没在控制台窗口中显示
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
void main()
{ //write text file
ofstream outFileObj;
outFileObj.open("E:\\text.txt",ios::out);
if(!outFileObj)
{
cerr<<"Create file faile";
exit(-1);
}
outFileObj << "Hello!\n";
outFileObj.close();
//read text file by binary
char * p;
p = (char *)malloc(7);
ifstream inObj("E:\\text.txt",ios::in|ios::binary);
if(!inObj)
{
cout << "can not open file";
abort();
return;
}
inObj.read(p, 7);
cout << p << endl; //为什么输出不到窗口中? 程序是在vs2008上可执行的
free(p);
p = NULL;
}
inObj.read(p, 7);
inObj.read(p, 6);
memset(p,0,7);//内存空间初始化下,不过最好不要用具体数字,
//只能读6个,因为最后一个必须是0,不然后cout不知道什么地方终止,直到遇见\0