|
-
May 3rd, 2002, 12:37 PM
#1
Thread Starter
Frenzied Member
search a file for a string [resolved]
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?
Last edited by markman; May 3rd, 2002 at 03:38 PM.
retired member. Thanks for everything 
-
May 3rd, 2002, 02:12 PM
#2
Frenzied Member
I just did an example like this in school
you can get each word by doing
Code:
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?
-
May 3rd, 2002, 02:58 PM
#3
Thread Starter
Frenzied Member
yeah thats the thing. it doesnt work.
do {
inFile >> szLine;
} while (szLine != "Customer");
It runs an infinite loop.....
retired member. Thanks for everything 
-
May 3rd, 2002, 03:01 PM
#4
Frenzied Member
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.
-
May 3rd, 2002, 03:11 PM
#5
Thread Starter
Frenzied Member
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".....
retired member. Thanks for everything 
-
May 3rd, 2002, 03:37 PM
#6
Thread Starter
Frenzied Member
figured it out. used a oostring instead of char[x]
retired member. Thanks for everything 
-
May 5th, 2002, 10:26 AM
#7
....
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|