Advertisement

Searching inside files

Started by August 04, 2000 12:57 AM
6 comments, last by ranxact 24 years, 4 months ago
I was wondering if there were any c functions for searching in file for a string.. (so i don''t have to write my own). i am useing FILE * ANSI c method for the file IO, but i don''t mind changing if necessary.. Thanx, RanXacT
RanXact@yahoo.com
Not in the ANSI standard. I guess you''ll have to write your own. Think of all the fun
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
Can''t you just open the file and treat the entire contents as a string? Then you could just use:


    #include <string.h>char string1[x] // make x equivalent to file sizechar string2[y] // size of string you are looking for (make sure                // to account for the null character)char *outstring;outstring = strstr(string1,string2);    


That should work fine for any plain text file.



BeS
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
Would that eat up lots of memory?
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
quote: Original post by Muzzafarath

Would that eat up lots of memory?


I don''t know. It''s in one of my books. I''m new to C/C++


BeS
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
if you are only gonna do this a once or just a few times then try this

    #include <string.h>char* string1;char* string2char *outstring;string1 = new char[sizeOfFile];string2 = new char[sizeOfStringLookingFor];outstring = strstr(string1,string2);// do all your program stuff here like load whatever you want from file then delete [] string1;delete [] string2;// cleans up all the memory used    



"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions



Edited by - ncsu121978 on August 4, 2000 5:11:02 PM
Advertisement
Woohoo. Looks like I was right for once. Maybe I am learning some of this stuff. Except the *''s. Oh well. I''m not into the "New/Delete" thing yet.

Just bought a copy of "C++ The Complete Reference" on advice from a friend. So I will be starting into that soon. Got it for $17.50 in a bargain bin at Hastings. Talked them down $7.50 because there was a place on the binding that was dented from it being dropped or crushed a bit. Just goes to show people, BE WILLING TO HAGGLE

BeS
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
you can do something like this (pseudo code)..

found = FALSE;

while not end of file
{
read a line from file
search for string using strstr() .. i.e. strstr(LineRead, SearchString)
if found set *found* to true and "break"
}

if ( found == TRUE )
{
// do whatever...
}

..-=gLaDiAtOr=-..

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

This topic is closed to new replies.

Advertisement