Results 1 to 7 of 7

Thread: search a file for a string [resolved]

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    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

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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?

  3. #3

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    yeah thats the thing. it doesnt work.
    do {
    inFile >> szLine;
    } while (szLine != "Customer");

    It runs an infinite loop.....
    retired member. Thanks for everything

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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.

  5. #5

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    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

  6. #6

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    figured it out. used a oostring instead of char[x]
    retired member. Thanks for everything

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ....

    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
  •  



Click Here to Expand Forum to Full Width