PDA

Click to See Complete Forum and Search --> : search a file for a string [resolved]


markman
May 3rd, 2002, 12:37 PM
Im trying to figure out how to search a file for a certain string, returning the byte (I can use seekg for the rest).

Say the file was:

Marketing
2
Billboards
17
TVAds
2
Customer
5
DDR_RAM
120
Monitors
52
Motherboards
23
CPU(Athlon)

It would return the byte offset of Customer.

How would I do that?

SteveCRM
May 3rd, 2002, 02:12 PM
I just did an example like this in school

you can get each word by doing

ifstream InFile("myfile.txt", ios::nocreate);

while(InFile>>mystring)
{
// check what each word is...if its the word you're looking for
// you can have a counter, even gets names, odds gets info
// or something like that
}


something like that?

markman
May 3rd, 2002, 02:58 PM
yeah thats the thing. it doesnt work.
do {
inFile >> szLine;
} while (szLine != "Customer");

It runs an infinite loop.....

SteveCRM
May 3rd, 2002, 03:01 PM
how did you declare szLine?

if you declared it for more characters than you're checking, maybe the items that are null is screwing up the check.

markman
May 3rd, 2002, 03:11 PM
I get a memory error if I try and make it a char*, so Im doing a char[256]. If I do the exact amount of chars in customer, then I cant fit other strings from the file like "Management".....

markman
May 3rd, 2002, 03:37 PM
figured it out. used a oostring instead of char[x]

CornedBee
May 5th, 2002, 10:26 AM
....

You can't compare C-style strings with ==. You need the function strcmp. If you use a string class, you should use string (declared in <string>), it's the only one that's in the C++ standard.