String replace in text file Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

String replace in text file

Post by Ugly! »

Hello wx people.
I'm making a code generator tool, and i have created for this .cpp and .h templates (not as in c++ templates). Where i want to replace certain string by others:
ie:

Code: Select all

Replace(wxT("%%CLASSNAME%%"),m_className));
I will also need to replace by a multiline string: ie:

Code: Select all

Replace(wxT("%%FOOCODE%%"),wxT("a=b;\nb=c;\n"));
So, my quiestion is, what is the best way to open a text file and replace this strings, and save it again.

Regards, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
Game_Ender
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Jun 15, 2005 5:47 pm

Post by Game_Ender »

Use regular expressions on buffers of text. If line by line is enough, just read in a line of text from the file and apply the regular expression(s) to it.
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

Post by Ugly! »

Game_Ender wrote:Use regular expressions on buffers of text. If line by line is enough, just read in a line of text from the file and apply the regular expression(s) to it.
Would i have to apply the replacements to each line???

How would i do this? (some example code maybe... ;) ) Isn't there a way to load the file in a string, Replace on the whole string with ReplaceAll= true??? I would use wxRegEx or the Replace method of wxString, is the same for my case.

But i would not like to do this on a line by line way.

Regards, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
User avatar
doublemax
Moderator
Moderator
Posts: 19163
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

wxFFile::ReadAll() can read a whole file into a wxString
Use the source, Luke!
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

Post by Ugly! »

doublemax wrote:wxFFile::ReadAll() can read a whole file into a wxString
Oops, i totally missed that one :oops: . I've always thought that wxFile and wxFFile were basecly the same...beeing the main the diference the eof method.

Well, I'll try this and go from here, i ll post some code for a finish concept.

Thanks, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
GeertVc
Super wx Problem Solver
Super wx Problem Solver
Posts: 273
Joined: Fri Sep 09, 2005 4:42 pm
Location: Belgium

Post by GeertVc »

Small remark, Mat
In commemoration of my beloved Mother...
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

Post by Ugly! »

[quote="GeertVc"]Small remark, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode