c/c++로 라인단위로있는 특수문제 몽땅 없애기 방법?

stypr의 이미지

텍스트를 라인단위로 읽어서 특수문자들만 모조리 없애는 쉬운 방법이 있을까요?
뭔가 있을거 같은디...

purewell의 이미지

ifstream ifs;
ifs.open("test.txt");

char buf[1024];
ifs.getline(buf, sizeof(buf));

일단 한 줄 읽기는 이렇게 하죠

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

cedar의 이미지

특수문자를 없애는 데, 굳이 라인 단위로 읽을 필요가 있나요?
그냥 문자 단위로 읽어도 되지 않나요?

그리고 '특수문자'와 '특수문자가 아닌 문자'를 무엇으로 정의하는 지도 중요하겠네요.

doldori의 이미지

대충 다음과 같이 되지 않을까요?

	const char* special_chars = "!@#$%^&";
	string line;
	while (getline(cin, line))
	{
		string::size_type pos = line.find_first_of(special_chars);
		while (pos != string::npos)
		{
			line.erase(pos, 1);
			pos = line.find_first_of(special_chars, pos);
		}
		cout << line << endl;
	}
moonzoo의 이미지

Quote:
그리고 '특수문자'와 '특수문자가 아닌 문자'를 무엇으로 정의하는 지도 중요하겠네요.

위 말에 올인~

한 character 씩 읽으면서

아스키 코드 값을 비교하는 것이 어떨까요.

whitekid의 이미지

tr 명령어를 적절히 사용하면 가능할 것 같은데요..

$ tr -cd "[:print:]" < file1 > file2

What do you want to eat?

moonzoo의 이미지

whitekid wrote:
tr 명령어를 적절히 사용하면 가능할 것 같은데요..

$ tr -cd "[:print:]" < file1 > file2

c/c++ 이 아니므로 무효~

cedar의 이미지

cedar wrote:
특수문자를 없애는 데, 굳이 라인 단위로 읽을 필요가 있나요?
그냥 문자 단위로 읽어도 되지 않나요?

그리고 '특수문자'와 '특수문자가 아닌 문자'를 무엇으로 정의하는 지도 중요하겠네요.

일단 '특수문자'는 간단하게 ispunct(c) == true 인 것으로 정의해보죠.
그러면, 영문자, 한글, 숫자를 제외하고는 모두 특수문자가 되어버리죠.

using namespace std;

int main()
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");

    remove_copy_if(
        istreambuf_iterator<char>(fin),
        istreambuf_iterator<char>(),
        ostreambuf_iterator<char>(fout),
        ispunct);
}