Sei sulla pagina 1di 6

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

911,392 Members | Top Members by Rank

JOIN DANIWEB

MEMBER LOG IN

C program to search within a file a source word and replace

Hardware & Software

Software Development

Web Development

Internet Marketing

Business Exchange

Community Center

Software Development > C > find and replace string in a text file

These search terms have been highlighted:

C program to search within a file a source word and replace it with a corresponding word

(Clear)

Ad:

Systems Management Software

C Discussion Thread

Unsolved

Views: 18349

somename
Newbie Poster Offline 17 posts since May 2008

May 24th, 2008

Permalink

find and replace string in a text file


Hello. I am trying to find out how to write a function to replace a string in a text file . So far i got this: find -> is a

Expand Post

word i am looking for replace , rep -> new word , f1

-> my file . And i'm stuck with that piece of code:

C Syntax (Toggle Plain Text)


1. while ( fgets( buff, BUFSIZ, f1 ) != NULL ) { 2. if ( strstr( buff, find ) != NULL ) {...

Similar Threads
Search and Replace in VB.NET Storing Words from Text File in Array in C++ pass the value to the text box in JavaScript / DHTML / AJAX

so i'm thinking about something like that:

Replacing text within a file using awk in Shell Scripting fstream in C

C Syntax (Toggle Plain Text)


1. strcpy(buff2, (buff - strlen(find) + strlen(rep) + 1); 2. fputs(buff2, f2)

where f2 is output file . So plz, maybe anyone can help me with this code?

Ads by Google

Get Ready for MAT


Free MAT Tests & Advice by Experts Get All Your Doubts Cleared on MAT minglebox.com/May-MAT

May 24th, 2008

Permalink

1 of 6

8/4/2011 11:25 PM

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

Re: find and replace string in a text file

-1

I don't get why you use fgets. If it 's an ASCII file , and Clockowl
Posting Whiz Offline 376 posts since May 2008

it should be for the thing you're trying to do, all you

need to do is strstr(buff, find) and then memcpy. Not strcpy, in an ASCII file aren't any NULL bytes. Well, not that I know of. I've never encountered NULL bytes in an ASCII file . So it 'd be more like this:

c Syntax (Toggle Plain Text)


1. //read in the whole file

2. char *ptr = strstr(buf, find); 3. memcpy(ptr, find, sizeof(char) * strlen(find));

Something like that.

Last edited by Clockowl; May 24th, 2008 at 10:37 am.

May 24th, 2008

Permalink

Re: find and replace string in a text file


jephthah
Posting Maven Offline 2,567 posts since Feb 2008

WHOA, whoa, whoa... Clockowl. That is so, SO wrong, I dont know where to start. Did you even try this? have you ever tried this? of course not, it would never even remotely work. It doesnt even make SENSE. IMHO, you need to read more and help less. a lot less.

Last edited by jephthah; May 24th, 2008 at 12:41 pm.

May 24th, 2008

Permalink

Re: find and replace string in a text file


jephthah
Posting Maven Offline 2,567 posts since Feb 2008

2 of 6

8/4/2011 11:25 PM

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

SOMENAME: C does not lend itself to easily extracting and replacing arbitrary text from a text field. if thats the case, then you can use a combination of functions like FSEEK, FTELL and REWIND. if its not the case ( and it tends to not be the case for typical applications), then you will need to : file . you can do it , only if the

field from which you are extracting/replacing is a FIXED WIDTH. there can not be any variation in the width of the

--open the original file for reading --open a new file for writing --read orig file one line at a time, modifing as needed --write that line to the new file . --when done, delete the original file --rename the new file with the original file 's name.

sucks, i know. if you really need to get into serious text file manipulation, you might look into Perl. but that's a whole 'nother can of worms.

Last edited by jephthah; May 24th, 2008 at 1:03 pm.

May 24th, 2008

Permalink

Re: find and replace string in a text file


Clockowl
Posting Whiz Offline 376 posts since May 2008

What's wrong about it ? Okay, it 's fixed length only, but that's like... obvious. I've parsed text in C . Never something like changing a string though, but I don't see why this method wouldn't work given it 's a fixed length string/word.

tesuji
Master Poster Offline 720 posts since Apr 2008

May 24th, 2008

Permalink

Re: find and replace string in a text file


To search for strings in ascii files there exists the amazing KnuthMorrisPratt string searching algorithm. It is

faster than anything. Try it , and you will be delighted.

May 25th, 2008

Permalink

Re: find and replace string in a text file


jephthah

3 of 6

8/4/2011 11:25 PM

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

Posting Maven Offline 2,567 posts since Feb 2008

SOMENAME: you're essentially on the right track, and you've basically got it . i think you're getting bogged down in some detail or other. check this out, and see how it 's pretty much what you're doing:

c Syntax (Toggle Plain Text)


1. #include <stdio.h> 2. #include <string.h> 3. 4. #define MAX_LEN_SINGLE_LINE 5. 6. int main() 7. { 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. } } fclose(fp2); fclose(fp1); return 0; } fputs(buff_ptr,fp2); fp1 = fopen(fileOrig,"r"); fp2 = fopen(fileRepl,"w"); while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1)) { buff_ptr = buffer; while ((find_ptr = strstr(buff_ptr,text2find))) { while(buff_ptr < find_ptr) fputc((int)*buff_ptr++,fp2); fputs(text2repl,fp2); buff_ptr += find_len; const char fileOrig[32] = "myOriginalFile.txt"; const char fileRepl[32] = "myReplacedFile.txt"; const char text2find[80] = "lookforme"; const char text2repl[80] = "REPLACE_WITH_THIS"; char buffer[MAX_LEN_SINGLE_LINE+2]; char *buff_ptr, *find_ptr; FILE *fp1, *fp2; size_t find_len = strlen(text2find); 120 // ?

it has a couple caveats... it requires that you #define the max length of a call also, it won't find a

file line. any that exceed that length

are at risk of missing an instance of the search text. could be a candidate for a carefully implemented malloc()

search text that is split across two lines. which would be a serious flaw in many situations.

Last edited by jephthah; May 25th, 2008 at 3:50 am.

4 of 6

8/4/2011 11:25 PM

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

somename
Newbie Poster Offline 17 posts since May 2008

May 26th, 2008

Permalink

Re: find and replace string in a text file


guys, thank you very much for your replies, especially jephthah, your code works great and i get it now where i got lost with my code! Thanks once again!

This thread is more than three months old


No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.

This thread is currently closed and is not accepting any new replies.

Previous Thread in C Forum Timeline: Next Thread in C Forum Timeline:

How to capture and store images using a web cam in C?

the math Log() function misbehaves

Tag Cloud for C


3 6 aes algorithm

array arrays
crash

assembly basic binary binarysearchtree binarytree buffer

c++

c-programming calculator calendar

code

comparing converter

cprogramming

csv c_programming

data-structure decimal double error fast fault fgets

file

filename files filter fopen free

function

functions game gcc getline

gimmetehcodez
multithreading

handling help! hexadecimal homework http image-processing lazy linked

linked-list linux list

loop makefile malloc

memory

mergesort multidimensional

newbie number shell sort winsock

parking path pointer

pointers
strings

problem program

programming
structures

pthread queue random records recursion recursive segmentation

segmentationfault

split stack statement

string

struct

structs structure

student syntax

tags-fail

text time triangle

unix

urgent variable

win32 windows

About Us | Contact Us | Advertise | Acceptable Use Policy


Forum Index | Build Custom RSS Feed

Follow us on

2010 DaniWeb LLC

5 of 6

8/4/2011 11:25 PM

find and replace string in a text file - C

http://www.daniweb.com/software-development/c/threads/125898

6 of 6

8/4/2011 11:25 PM

Potrebbero piacerti anche