ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.