473,327 Members | 1,997 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,327 software developers and data experts.

Find / Replace string in text file

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 8302
sicarie
4,677 Expert Mod 4TB
Have you tried using getline() and strtok()?
Aug 27 '07 #2
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 Expert Mod 4TB
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
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 2GB
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
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 =...
1
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
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...
4
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
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
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...
0
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...
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...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.