如何用fstream读取空格(推荐!!!)

很早之前就想写点什么东西,可是一直都没有这个心静下来,最近终于有时间了,想想自己的水平也不算什么,还敢在网上发表谬论,深感惶恐,希望高手们不要扔我砖头,也希望这点东西对新手有些许帮助。大家共同学习共同进步吧。 

我想c++的文件操作也不是很难,想必大家都会,所以今天就说说我以前遇到过的一些问题,希望对大家有用。 

设定:我打算要将一篇英文文章读取到内存中,顺便可能对文件做一些操作。 

我挺喜欢《海上钢琴师》这部电影的,就以这部电影的一个英语影评(部分)来做测试文档吧:

He's a man so brave, that he can play the imaginary piano when exploded with the ship he was born, lived and died in. Yet he's a man so scared, that he cannot face the infinite city life we are living everyday. He's a man so intelligent, that he can play the piano as if he has four arms (or I'd rather say, he has God's arms). 

Yet he's a man so stupid, that he chose to gone with the wind while he's other choice could be marrying a beautiful woman and having a child. He's a man so perceptive, that he could use his music language to describe exactly others' feelings. Yet he's a man so insensitive that he'd rather disappear after a gentle kiss on his beloved while she's asleep than unburden himself and tell her "ILU". 

就习惯性的命名为input.txt吧. 

根据我们的目标,两年多以前我会这么写: 

#include <iostream> 

#include <fstream> 

using namespace std; 


int main() 



 char ch[1000]; 

 ifstream ifs("input.txt"); 

 if( ifs.fail() ) 

 return -1; 

 int i=0; 

 char c; 

 for(;!ifs.eof();i++ ) 

 { 

 ifs>>c; 

 ch[i] = c; 

 } 

 ch[i] = '\0'; 

 ifs.close(); 

 i=0; 

 while( ch[i] != '\0' ) 

 { 

 cout<<ch[i++]; 

 } 

 return 0; 



程序没有错误,运行也可以,不会崩溃,而且也能读取出字符来,可惜读取出来是这样的效果: 

此处限制,步伐了,大家可以猜到是什么 



这可不是我要的效果,空格呢?换行呢?谁吃去了? 

其实呢,是被>>给吃了,它把空格和换行符都吃掉了。还有一个不妥在于数组的分配,如果字符数大于了1000,怕是要出问题哦! 

当然,我们可以用这样的方法来解决: 

ifs.seekg(0,ios::end); 

int size = ifs.tellg(); 

哈哈,动态分配,你可真聪明。可是动态分配有能怎样呢?我们还是不能读取到空格和换行啊。 

别着急,一句话就搞定了,我们把下面这句话添加到刚才的代码中去: 

ifs>>noskipws; //放在读取的前面 

哈哈,貌似成功啦!呵呵。。 

这样,我们的目标就差不多达到了,不过我们今天可不能就这点吧?是的,你猜对了,我们再继续探寻别的方法,大家都知道string吧?好,有了它,我们就N好办事了哦。看招: 

#include <iostream> 

#include <fstream> 

#include <string> 

#include <iterator> 


using namespace std; 


int main() 



 ifstream ifs("input.txt"); 

 if( ifs.fail() ) 

 return -1; 

 string ss; 

 char ch; 

 while( !ifs.eof() ) 

 { 

 ifs.get(ch); 

 ss.push_back(ch); //自己用VC7.0以上的版本哦,否则用vector<char>代替string 

 } 

 ifs.close(); 

  

 copy( ss.begin(),ss.end(),ostream_iterator<char>(cout,"")); 

 //for( string::iterator it=ss.begin();it!=ss.end();it++) 

 // cout<<*it; //一个作用用for_each也是可以的 


 return 0; 



运行下丫,行了吧?呵呵。 

我看着while循环很不顺眼,你呢?好讨厌哦,呵呵,去掉它,真烦人: 

#include <iostream> 

#include <fstream> 

#include <string> 

#include <iterator> 


using namespace std; 


int main() 



 ifstream ifs("input.txt"); 

 if( ifs.fail() ) 

 return -1; 

 string ss; 

 ifs.unsetf( ios::skipws ); 

 copy( istream_iterator<char>(ifs),istream_iterator<char>(),back_insert_iterator<string>(ss) );  

 ifs.close(); 

  

 copy( ss.begin(),ss.end(),ostream_iterator<char>(cout,"")); 


 return 0; 



怎么样?还正常吧? 


呵呵,什么?你还嫌代码多?好,看来不出绝招是不行的啦。 

#include <iostream> 

#include <fstream> 

#include <string> 

#include <iterator> 


using namespace std; 


int main() 



 ifstream ifs("input.txt"); 

 if( ifs.fail() ) 

 return -1; 

 string ss( ( istreambuf_iterator<char>(ifs)),istreambuf_iterator<char>()); 

 ifs.close(); 

  

 copy( ss.begin(),ss.end(),ostream_iterator<char>(cout,"")); 


 return 0; 



这下你总满意了吧?还不满意我也没办法啦。。。今天到此结束,欢迎各位光临! 

希望各位高手还有什么其它的好方法不要吝啬,拿出来分享吧。 



http://hi.baidu.com/_%E2d_%B7%B3_%DE%B2%C2%D2/blog/item/c66efc86483fad3a67096eb7.html  

原帖完整版。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值