473,652 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find / Replace string in text file

4 New Member
Hi All,

Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the input file should be retained in output file. And, if possible we should be able to replace multiple search strings with multiple replace strings. e.g;
srch1, srch2, srch3 in in.txt should be replaced by rplc1, rplc2, rplc3 in out.txt

Thanks.
Aug 27 '07 #1
5 8342
sicarie
4,677 Recognized Expert Moderator Specialist
Have you tried using getline() and strtok()?
Aug 27 '07 #2
neeludhiman
4 New Member
Hi,

I forgot to mention that I need the code for Windows platform. So I can not use sed, getline() as they are not available in Windows.

Thanks.
Aug 29 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Hi,

I forgot to mention that I need the code for Windows platform. So I can not use sed, getline() as they are not available in Windows.

Thanks.
I'm pretty sure getline is available in Windows, what headers are you including in your program?
Aug 29 '07 #4
neeludhiman
4 New Member
Solved:

while (fgets(str, 80, infile) != NULL) {
while ((srchstr = strstr (str, "my")) != NULL)
memcpy(srchstr, "yo", strlen("yo"));
while ((srchstr = strstr(str, "this")) != NULL)
memcpy(srchstr, "that", strlen("that")) ;
fputs(str, outfile);
}
Aug 30 '07 #5
dmjpro
2,476 Top Contributor
Solved:

Expand|Select|Wrap|Line Numbers
  1.  while (fgets(str, 80, infile) != NULL) {
  2.        while ((srchstr = strstr (str, "my")) != NULL)
  3.            memcpy(srchstr, "yo", strlen("yo"));
  4.        while ((srchstr = strstr(str, "this")) != NULL)
  5.            memcpy(srchstr, "that", strlen("that"));
  6.        fputs(str, outfile);
  7. }
  8.  
Please follow the guideline while you do post.

Kind regards,
Dmjpro.
Aug 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
33672
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 = string.find(file(os.path.join(root,fname), 'rb').read(), 'THIS') print find
1
3719
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
19
2924
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your company's clients. Your company has generated its own unique ID numbers to replace the social security numbers. Now, management would like the IT guys to go thru the old data and replace as many SSNs with the new ID numbers as possible. You...
4
17470
by: Vic | last post by:
Hi, With VBA code from MSAccess, how to search for a string in another text file, and replace it with space and then save it under the same name? Your help is greatly appreciated. Rgds
25
4052
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
0
3075
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 want to replace all of them. You need to look at it in a case-by-case basis. What can you do? Answer: emacs.
0
1615
by: Dmitridj | last post by:
Heya everyone, I'm trying to write a program that finds and replace an array of characters defined by the user in a text file. Basicly the program finds and replaces a substring typed in by the user. I've got the opening file and displaying its contents down, but the problem is, I can not use any of the string member functions to find and replace. I am trying to read a line of text from the input file at a time and comparing it to what...
3
16914
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 with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
3
14552
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 same length as Replace string. Here's what I have so far. I spent many hours googling for sample code but couldn't find much. Thanks... public static void FindReplace(string OldFile, string NewFile) { string sFind = "ABC"; //I probably need...
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.