读书人

怎么将大文本文件中的空格替换为特殊字

发布时间: 2012-02-19 19:43:39 作者: rapoo

如何将大文本文件中的空格替换为特殊字符??
我有一文本文件:
3.0.0.0 3.255.255.255 美国 新泽西通用电气公司
4.0.0.0 4.10.255.255 美国 CZ88.NET
4.11.0.0 4.11.255.255 美国 夏威夷
4.12.0.0 4.19.77.255 美国 CZ88.NET
4.19.78.0 4.19.78.255 美国 西南政法大学
4.19.79.0 4.19.79.63 美国 Armed Forces Radio/Television

因为我要把这些数据导入到数据库中,由于格式不对,所以我想把他们处理下,想请教大家如何将这些数据中空格替换特殊字符比如”;“
替换后变成:
3.0.0.0;3.255.255.255;美国;新泽西通用电气公司
4.0.0.0;4.10.255.255;美国;CZ88.NET
4.11.0.0;4.11.255.255;美国;夏威夷
4.12.0.0;4.19.77.255;美国;CZ88.NET
4.19.78.0;4.19.78.255;美国;西南政法大学
4.19.79.0;4.19.79.63;美国;Armed Forces Radio/Television
最后一行后面的Armed Forces Radio/Television 中的空格不变。


[解决办法]

有ultraEdit吗?
用ultraEdit打开文件,选择 "搜索 "菜单下的 "替换 ",在查找栏中填入
[ ]+
替换为栏中添
;
在下面的复选框中将正则表达式选中,然后单击全部替换即可

一个十几兆的文本文件替换也就几秒的功夫吧
[解决办法]
#include <fstream>
#include <string>
#include <iostream>
#include <windows.h>

using namespace std;


int main()
{

fstream Rdfile( "test.txt ");
fstream Wrfile( "test1.txt ", ios_base::in|ios_base::out|ios_base::ate);

if (!Rdfile || !Wrfile )
{
exit(0);
}

string strBufOne;
string strBufTwo;
string strBufThree;
string strBufFour;
string strBufFive;

DWORD time1 = GetTickCount();

for (int i = 0; i < 100000; i++)
{
while (!Rdfile.eof())
{
Rdfile > > strBufOne;
Rdfile > > strBufTwo;
Rdfile > > strBufThree;
Rdfile > > strBufFour;
getline(Rdfile, strBufFive);
//这里可能要改进
strBufOne = strBufOne + ": " +strBufTwo + ": " + strBufThree + ": " + strBufFour + strBufFive;
Wrfile < < strBufOne < < '\n ';
}

Rdfile.clear();
Rdfile.seekg(0, ios_base::beg);
}

DWORD time2 = GetTickCount();

cout < < (time2 - time1) < < endl;

Rdfile.close();
Wrfile.close();

return 0;
}

= =你用我的测试下需要多少时间,,我看看性能怎么样,,,,...感觉好象我的好慢

读书人网 >C++

热点推荐