Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.daniweb.com/software-development/c/threads/125898
JOIN DANIWEB
MEMBER LOG IN
Software Development
Web Development
Internet Marketing
Business Exchange
Community Center
Software Development > C > find and replace string in a text file
C program to search within a file a source word and replace it with a corresponding word
(Clear)
Ad:
C Discussion Thread
Unsolved
Views: 18349
somename
Newbie Poster Offline 17 posts since May 2008
Permalink
Expand Post
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
where f2 is output file . So plz, maybe anyone can help me with this code?
Ads by Google
Permalink
1 of 6
8/4/2011 11:25 PM
http://www.daniweb.com/software-development/c/threads/125898
-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
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:
Permalink
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.
Permalink
2 of 6
8/4/2011 11:25 PM
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.
Permalink
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
Permalink
Permalink
3 of 6
8/4/2011 11:25 PM
http://www.daniweb.com/software-development/c/threads/125898
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:
it has a couple caveats... it requires that you #define the max length of a call also, it won't find a
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.
4 of 6
8/4/2011 11:25 PM
http://www.daniweb.com/software-development/c/threads/125898
somename
Newbie Poster Offline 17 posts since May 2008
Permalink
This thread is currently closed and is not accepting any new replies.
array arrays
crash
c++
code
comparing converter
cprogramming
csv c_programming
file
function
gimmetehcodez
multithreading
memory
mergesort multidimensional
pointers
strings
problem program
programming
structures
segmentationfault
string
struct
structs structure
student syntax
tags-fail
unix
urgent variable
win32 windows
Follow us on
5 of 6
8/4/2011 11:25 PM
http://www.daniweb.com/software-development/c/threads/125898
6 of 6
8/4/2011 11:25 PM