search for a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madshov
    New Member
    • Feb 2008
    • 20

    search for a string

    Hi everyone,
    Does there exist a function to seach for a string in a file, when I have a pointer to a character array:

    Code:
    ifstream infile = f.c_str();
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    No. Strings don't exist in files. Data exists in files and you have to read the data into your array and interpret it.

    That requires you know the format of the data in the disc file.

    Say, if it's a text file, you read a record into a buffer using the read method or use the getline() function to do it for you. Then you can convert the buffer into a string object.

    Then you can compare what you read with what you are looking for and if that wasn't it you read again and repeat the process until you read end of file.

    If you know the STL you might try using input iterators with the find() algorithm. It might save you writing the above code yourself.

    Comment

    • madshov
      New Member
      • Feb 2008
      • 20

      #3
      Thanks for the reply. I figured it out!

      Comment

      Working...