Find first of operation on a char pointer
I have a char buffer which contains characters read from a file. I need to take this char buffer and find the first end of line character within it.
EOL characters in this case are \n,\r,\f.
Initially, I was planning to do the following:
// let's read into our buffer now...
char * m_pLineBuff;
if(!readCharBuf(m_pLineBuff, bytesToRead)) { report("Could not fill line buffer", RPT_ERROR, __PRETTY_FUNCTION__); }
// setup our newline candidates in an array
int iEOLChars[] = {'\n','\f','\r'};
// find the first instance of a newline character
in开发者_如何学Pythont iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);
However, I apparently cannot pass a char pointer to the std::find_first_of
method -- I can only pass an integer. The exact error the compiler provides me is:
error: invalid conversion from ‘char*’ to ‘int’
This seems strange to me, as I've defined the start and end locations of my char buffer and I do not understand why it could not iterate through them looking for the first occurrence of any of my EOL characters.
Any advice on how to resolve this? Is there a way to use find_first_of
, or should I simply iterate through each position of the char buffer and check to see if the char at the location matches any of my EOL characters.
The "find_first_of" function I am referring to is this one: http://www.cplusplus.com/reference/algorithm/find_first_of/
Any assistance is always appreciated.
The function find_first_of
returns, in this case, a pointer, not an index, so try:
char *iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);
I think the problem is a type mismatch here:
char * m_pLineBuff;
int iEOLChars[] = {'\n','\f','\r'};
Try it declaring your iEOLChars
as a char
array.
Check your first_first_of function I think it can never have 4 parameters
Refer first_first_of
精彩评论