|
내친 김에...
STL을 써서 하는 방법을 소개해드리죠.
여기서의 예제 코드는 윈도 프로그램이 아닌 콘솔 프로그램으로 구현했습니다.
물론 윈도 프로그램으로도 쉽게 변환할 수 있습니다.
아래에 제가 TMemo::Lines 나 TListBox::Items 의 TStrings 타입의 속성을 사용한 방법이나,
유영님께서 소개하신 TStringList (TStrings의 자손 클래스입니다.
TStrings 자체로는 인스턴스화 할 수가 없고, 대신 그 자손인 TStringList를 씁니다.)
를 사용하는 방법은,
텍스트 파일을 통째로 메모리에 올려두고 쓰는 방법입니다.
만약, 텍스트가 너무 커서 메모리에 전부 로드할 수 없다면,
다음과 같이
istream_iterator, ostream_iterator 반복자와
remove_copy 알고리듬을 사용하는 방법이 있습니다.
이 경우는 입력 파일과 출력 파일을 다르게 해야합니다.
//---------------------------------------------------------------------------
// remove_copy.cpp
//---------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#pragma hdrstop
#include <string>
#include <iterator>
#include <algorithm>
//---------------------------------------------------------------------------
using namespace std;
#pragma argsused
int main(int argc, char* argv[])
{
if (argc < 3) {
cerr << "usage: remove_copy inputfile outputfile\n";
exit(1);
}
ifstream fin(argv[1]);
ofstream fout(argv[2]);
istream_iterator<string> end_of_stream;
remove_copy(istream_iterator<string>(fin), end_of_stream,
ostream_iterator<string>(fout, "\n"), string("아무개"));
return 0;
}
//---------------------------------------------------------------------------
|