replace string in file

G

Guest

Hello World,

I am trying to replace a string in a large file without going through the
process of reading every line to locate the string in question. Is there a
function which will do this? I was looking into the FindReplace method of the
Find object, but couldn't find an example showing all the stuff that needs to
be added to the C++ code to use it.
In accordance with some sites on using DTE objects I've added the following
to my source file:

#pragma warning( disable : 4278 )
//The following #import imports DTE based on its LIBID
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0")
lcid("0") raw_interfaces_only named_guids
#pragma warning( default : 4278 )
using namespace EnvDTE; // optional

CComPtr<EnvDTE::_DTE> m_pDTE;
HRESULT hr = m_pDTE.CoCreateInstance("VisualStudio.DTE.7.1", 0, CLSCTX_ALL);

Find change;

change.FindReplace(vsFindAction.vsFindActionReplace,"<name>412<",NULL,"<Style>\n<iconstyle>\n<color>ff076aff</color>\n</iconstyle>\n</style>\n<name>412<",vsFindTarget.vsFindTargetFiles,KMLCounty,NULL,vsFindResultsLocation.vsFindResultsNone);

but when I try to compile the code I get errors like:

d:\data\dohertys\google earth\bridgedata\bridgedataDlg.cpp(180): error
C2039: 'vsFindResultsNone' : is not a member of 'System::Enum'
d:\data\dohertys\google earth\bridgedata\bridgedataDlg.cpp(170): error
C2259: 'EnvDTE::Find' : cannot instantiate abstract class
....

and lots of others

Does anyone have either some sample code to set this up, or an efficient
alternative?

Thanks,
Stephanie
 
M

Mihai N.

I am trying to replace a string in a large file without going through the
process of reading every line to locate the string in question. Is there a
function which will do this?
This is not possible.
You might find a library or a function to do this, but that function or
library will do the reading, so if your problem is performance, then
there is not much you can do.
The only exception is if all lines are of equal length, if the line after
the replacement is also the same length, and you know a lot about the file
(for instance that is sorted).


DTE is part of the Visual Studio object model, so it is not available on
systems without Visual Studio installed. If you are writing a VS extension
this is fine, but otherwise ...
 
Top