你的代码没法编译通过,此外,"D:\old.txt"肯定是错的,stoul也没考虑数字很大的情况
修改你的源代码为

程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>
using namespace std;
void foo( const std::string& filename, int isxxx(int c), std::ostream& out )
{
std::ifstream istr( filename );
if( istr )
{
for( std::string tmp; getline(istr,tmp); )
{
for( std::string::const_iterator p=tmp.cbegin(); p=find_if(p,tmp.cend(),isxxx), p!=tmp.cend(); )
{
std::string::const_iterator q = find_if_not( p, tmp.cend(), isxxx );
std::copy( p, q, std::ostream_iterator<char>(out) ) = ' ';
p = q;
}
}
}
out << '\n';
}
int main( void )
{
foo( R"(D:\old.txt)", ::isdigit, cout );
foo( R"(D:\old.txt)", ::isalpha, cout );
}
不明白为什么要以“行”为缓冲?

程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>
int main( void )
{
std::ifstream fin( R"(D:\old.txt)");
std::ofstream fo1( R"(D:\new1.txt)");
std::ofstream fo2( R"(D:\new2.txt)");
if( !fin || !fo1 || !fo2 )
return 1;
for( char ch, drt=0; fin.get(ch); )
{
if( ::isdigit(ch) )
{
if( drt != 1 )
fo1.put( ' ' );
drt = 1;
fo1.put( ch );
}
else if( ::isalpha(ch) )
{
if( drt != 2 )
fo2.put( ' ' );
drt = 2;
fo2.put( ch );
}
else
drt = 0;
}
}