tags:

views:

57

answers:

4

I want to find some words after i get the whole file to char*. I know how to do it using the string class functions but i don't want to copy the data again to a string variable. is there any similar functions available to use for char* strings or should i still use string class?

+2  A: 

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

The string manipulation functions for basic strings are situated in the cstring / string.h header: http://www.cplusplus.com/reference/clibrary/cstring/

Ronny
Please do not simply post a link as an answer. Provide at least a sentence explaining what it is linking to.
anon
You are right, but the docs should be self explanatory. A pointer to the solution should be enough.
Ronny
A: 

You can search using strstr (for one example). If the data is large enough that the copying time is significant, it would probably be worth using something like a Boyer-Moore-Horspool search.

Jerry Coffin
A: 

Why don't you just load the file into a string in the first place since you know how to use it?

#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>

int main( int argc, char* argv[] ){
std::ifstream ifsFile( <file_name> );
ifsFile.unsetf( std::ios_base::skipws ); // make sure we're not skipping whitespaces

std::istream_iterator< char > begin( ifsFile ), end;
std::string strFile( begin, end ); // load the file into the string

// now print the file/search for words/whatever
std::copy( strFile.begin(), strFile.end(), std::ostream_iterator< char >( std::cout, "" ) );

return 0;
}
Eugen Constantin Dinca
Rather than strstr(str,"find") ?
Martin Beckett
Eugen Constantin Dinca
A: 

You will likely need to use more than strstr() because you'll probably be interested in word boundaries (I;m guessing that you'll need to find matches that are bounded by whitespace or the start/end of the line).

Various standard library functions that might be of interest:

strstr()
strtok() (or it's non-standard but sill useful cousin strtok_r())
strspn()
strpbrk()
strcspn()
strchr()

But, string parsing is a real pain in straight C (in my experience), you may want to look for an existing parsing library (or regex library) that might help mitigate the pain.

Michael Burr