How do you search inside a text file

Started by
20 comments, last by kingpinzs 18 years, 11 months ago
I am loading a text file and i want it to load info right after L1: How can I do that?
Advertisement
You could load the whole file into a string and then use "find_first_of()"

You could jump from line to line, and assuming the L1: is at the beginning of the line, use "peek()" to look for 'L' and then see if it's "L1:"

You could put everything after L1: into its own file so you don't have to search for it.

Edit: that was really wierd those two posts
A bit more detail would be helpful. Does that stand alone on it's own line? Is it embedded in some text?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I dont know but that does not sound very afficent because I will need to load a lot of text files and to help keeping the load times low I just wanted to load at a sertin point in the text file.

Thanks For the help

EDIT: it is on a line by it's self then I need all the text below it to be read tell another token/word L2:, L3:, Obj:, NPC: then it keeps going to end of file also I want each text file to be a complete level so I only have to read one text file per level.
If you don't store the position ahead of time, you're going to have to search everything before it to find "L1:" Then once you have the position use "seekg()" to start reading from that point on.
It sounds like instead of just searching for it, you should just read the file a bit at a time. You probably want the information before it.

This might be off, but it would go like this:

read first part of fileloop until eof   read line   switch on line   case(L1) ....   case(L2) ....   case(Obj)....


Or you could load the whole file into something like a set with the type (L1, L2, Obj) being the key.
Boder has the right idea. You just read the file line by line, test the line if it's the string you're looking for ("L1" in your case) and if it is, then you start doing whatever you want to the data that follows.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

could Some one show me sample code on how to do this in console mode in c?

Thanks For the help
One question...are you using stdio or fstream for your file io?


-SirKnight
I am using fstream

ifstream MapFile("level1.txt");

This topic is closed to new replies.

Advertisement