473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

replace string in file

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.vsFindActionReplac e,"<name>412<",NULL,"<Style>\n<iconstyle>\n<color> ff076aff</color>\n</iconstyle>\n</style>\n<name>412<",vsFindTarget.vsFindTargetFiles ,KMLCounty,NULL,vsFindResultsLocation.vsFindResult sNone);

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
Jun 22 '06 #1
1 1362
> 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 ...
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
17
by: Levidikus | last post by:
Normally, I never have any problems with String.Replace(). However, I found that I need to replace multiple instances of the character "ª" (\xAA) with a # symbol. The input file is a simple one...
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.