LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2009, 10:57 AM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Rep: Reputation: 70
Search and replace strings in text files in C


HI all,

Which C standard library functions should I be looking at to search a large text file (html source)for a given string and insert text where required. Please don't suggest grep, because the strings to be found and written are in different places within the file!
Thanks!
cc.
 
Old 09-15-2009, 11:52 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
google is your friend


http://www.daniweb.com/code/post968900.html
 
Old 09-15-2009, 01:47 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It depends greatly on the nature of the data, and also upon the nature of your application.
The principle elements of your objective are string handling, file IO, and possibly memory management.

On the matter of string handling, you will need to do searching, adding &/or deleting of character data. If there are specific, constant, strings that are being searched, then the usual suspects would be strcmp() and it's kin. As often as not, the task is a bit more complex, and so may involve such things as regular expressions. This would imply either using an external library such as libpcre, or generating a standard-C parser using standard tools such as lex & yacc (or, in GNUese, flex & bison).

File IO can be accomplished with either of two families of functions: the open()/read()/write()/close(), or the similar stream-oriented fopen()/fread()/fprintf()/fclose() functions. You may want to use some kind of file and directory handling for finding files, and maybe creating backups and or temporary files. For these purposes, there are many helpful functions, mostly enumerated in stdio.h. If the program is to be run stand-alone, it is probably easiest to code as a filter: that is reading stdin and writing stdout and stderr.

A third matter may crop up, depending on how the first two are handled, that being memory management. In some way, you will need to read data from files, modify the data in memory, and write data back to files. How the data is stored and managed in-memory will be part of your application, and may involve the likes of malloc()/calloc()/realloc(), etc.

Since you are not even close to being the first person to have requirements like you have listed, I would urge you to re-consider whether you really want to undertake the project. Given the naive nature of your questions, it is improbable that you could approach the quality of existing standard applications that already serve exactly the purpose that you have identified. sed and awk would certainly lead the list in that respect.

The universe of text handling code is much smaller than the universe at large, and with a modest amount of study, one can become Completely Clued In.

--- rod.

Last edited by theNbomr; 09-15-2009 at 06:51 PM.
 
Old 09-15-2009, 03:04 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
rpl is excellent for such things, it'll even handle doing things recursively through folders for changing text in multiple files
 
Old 09-15-2009, 03:15 PM   #5
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Thank you, Rod. Useful observations. I'm aware of awk and sed (I meant to say sed when I said grep!). But these won't give me the exercise I badly need and in any case,they don't do exactly the job I have in mind. I don't want EVERYTHING done for me. I'd sooner write a pair of custom functions to do the task by hacking some existing code. That way I get the intellectual challange and learn something. I only really want some initial direction. Even some ideas for a workable algorithm would be welcome.
 
Old 09-15-2009, 06:51 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I didn't realize that this was to be principally an academic exercise. In that case, code away. The problem you have chosen to solve will give you experience working with very useful cross section of the standard C library. Do note, also, that if this were a real-world problem, your approach would probably not fit in the top ten most efficient implementations, at least as measured on an effort/reward ratio.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep search & replace strings with backslashes? ziphem Linux - Newbie 4 10-21-2007 04:36 PM
How to search for text strings? armandino LQ Suggestions & Feedback 2 06-24-2007 07:51 PM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration