去掉TXT文档中的空格 换行和标点符号

dian729506189 2012-04-19 05:18:21
编程实现去除txt文档中所有的空格 和换行。就类似于这样:

庆历四年春,滕子京 谪守巴陵 郡。越明
年,政通人和,
百废具兴。乃重 修岳阳楼 ,增其旧制, 刻唐贤今人
诗赋于其上。属予作文
以记之。

去除以后应该变为:庆历四年春,滕子京谪守巴陵郡。越明年,政通人和,百废具兴。乃重修岳阳楼,增其旧制,刻唐贤今人诗赋于其上。属予作文以记之。

求大神指导,本人一菜鸟。最好C++实现!!
...全文
1219 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
编译能通过,但是结果是乱码!
a.txt里面编程乱码,out.txt没见出来!!

引用 8 楼 evencoming 的回复:
引用 7 楼 的回复:
引用 6 楼 的回复:
编译不过、、、

引用 3 楼 的回复:
C/C++ code


#include<itreator>
#include<algorithm>
#include<fstraem>
#incl……
[/Quote]
你不是说要原来的文件吗?不要out.txt吗?
所以remove 这个调用给删除了。。
乱码问题我看看。。还是得找编译器。。
dian729506189 2012-04-19
  • 打赏
  • 举报
回复
编译能通过,但是结果是乱码!
a.txt里面编程乱码,out.txt没见出来!!
[Quote=引用 8 楼 evencoming 的回复:]
引用 7 楼 的回复:
引用 6 楼 的回复:
编译不过、、、

引用 3 楼 的回复:
C/C++ code


#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filen……
[/Quote]
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
错误太多 ,越调越出错。。!

引用 4 楼 evencoming 的回复:
引用 3 楼 的回复:
C/C++ code

#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *fi……
[/Quote]
看8L,,手头没编译器的后果。。
靠眼界查错。。
dian729506189 2012-04-19
  • 打赏
  • 举报
回复
嗯,这个能用!谢啦!!
[Quote=引用 9 楼 hen_hao_ji 的回复:]
C/C++ code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ifstream in("1.txt", ios::in);
ofstream out("2.txt", ios::out);

string str……
[/Quote]
dian729506189 2012-04-19
  • 打赏
  • 举报
回复
错误太多 ,越调越出错。。!
[Quote=引用 4 楼 evencoming 的回复:]
引用 3 楼 的回复:
C/C++ code

#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.……
[/Quote]
hen_hao_ji 2012-04-19
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ifstream in("1.txt", ios::in);
ofstream out("2.txt", ios::out);

string str;
while(in&&in>>str)
{
out<<str;

}

return 0;
}
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
引用 6 楼 的回复:
编译不过、、、

引用 3 楼 的回复:
C/C++ code


#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
……
[/Quote]
[code=C/C++]
#include<cstdio>
#include<itreator>
#include<algorithm>
#include<fstream>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.txt";
remove_copy_if(
istreambuf_iterator<char>(ifstream(filename,ios_base::binary)),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(ofstream(outname,ios_base::binary)),
isspace);
remove(filename);
rename(outname,filename);
return 0;
}

[/CODE]
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
编译不过、、、

引用 3 楼 的回复:
C/C++ code


#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *……
[/Quote]
#include<fstraem>
头文件写错了。
还少了个 cstdio
改成下面这样、吧:
[code=C/C++]
#include<cstdio>
#include<itreator>
#include<algorithm>
#include<fstream>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.txt";
remove_copy_if((istreambuf_iterator<char>(ifstream(filename,ios_base::binary)),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(ofstream(outname,ios_base::binary)),
isspace);
remove(filename);
rename(outname,filename);
return 0;
}
[/CODE]
applegeeE 2012-04-19
  • 打赏
  • 举报
回复
编译不过、、、[Quote=引用 3 楼 的回复:]
C/C++ code


#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.txt";
remo……
[/Quote]
querdaizhi 2012-04-19
  • 打赏
  • 举报
回复

#include<stdlib.h>
#include<stdio.h>
#include<string.h>


int main()
{
FILE *fp = fopen("test.txt" , "r");
char buffer[1024];
memset(buffer , '\0' , sizeof(buffer));
int size = fread(buffer,sizeof(char) ,1024 , fp);

for(int i=0;i<size;i++)
{
if(buffer[i] == ' ' || buffer[i] == '\n')
continue;
else
printf("%c" , buffer[i]);
}
return(0);
}
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
C/C++ code

#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.txt";
remove……
[/Quote]
少了个头文件
#include<cstdio>
evencoming 2012-04-19
  • 打赏
  • 举报
回复
[code=C/C++]
#include<itreator>
#include<algorithm>
#include<fstraem>
#include<cctype>
using namespace std;

int main()
{
const char *filename="a.txt";
const char *outname="out.txt";
remove_copy_if((istreambuf_iterator<char>(ifstream(filename,ios_base::binary)),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(ofstream(outname,ios_base::binary)),
isspace);
remove(filename);
rename(outname,filename);
return 0;
}
[/CODE]
dian729506189 2012-04-19
  • 打赏
  • 举报
回复
这个随便,重新生成一个文件也行,但是我觉得最好是在源文件的基础上直接去除就行,这样方便我后期的处理!![Quote=引用 1 楼 的回复:]

要保存到一个文件吗?
[/Quote]
hen_hao_ji 2012-04-19
  • 打赏
  • 举报
回复
要保存到一个文件吗?

70,038

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧