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