注册 登录
编程论坛 C++教室

I/O的问题!

wangzm07 发布于 2007-09-20 13:59, 443 次点击

本人自学C++,首次发贴,希望不是浪费各大虾时间。
问下面的程序输出没有空格怎么回事,怎么把空格弄出来?(环境VC6.0)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string st("hello world!");
ofstream ofile("file");
if(!ofile)
{cerr<<"error:unable to open input file;"<<ofile<<endl;
return -1;
}
ofile<<st<<endl;
ofile.close();


ifstream ifile("file");
if(!ifile)
{cerr<<"error:unable to open input file;"<<ofile<<endl;
return -1;
}
char a;
while(ifile>>a)

cout<<a<<flush;
cout<<endl;
return 0;

}

4 回复
#2
wangzm072007-09-20 14:10
空格指hello与world之间的空格。
#3
HJin2007-09-20 14:18
skipws and noskipws manipulators.
#4
aipb20072007-09-20 15:59
ifstream那,你一次读取一行,再输出不就有了!
#5
hzdz2007-09-20 17:46

我这两天也看在看这个,顶楼主,如四楼版主所说:你用string input_str;getline(ifile,input_str);
比较省事.

1