在char指针上找到第一个操作

BSc*_*ker 2 c char eol

我有一个char缓冲区,其中包含从文件中读取的字符.我需要使用此char缓冲区并在其中找到第一行结束符.

在这种情况下,EOL字符是\n,\ r,\ f.

最初,我打算做以下事情:

// 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
int iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);
Run Code Online (Sandbox Code Playgroud)

但是,我显然无法将char指针传递给该std::find_first_of方法 - 我只能传递一个整数.编译器为我提供的确切错误是:

error: invalid conversion from ‘char*’ to ‘int’
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很奇怪,因为我已经定义了我的char缓冲区的开始和结束位置,我不明白为什么它不能遍历它们寻找我的任何EOL字符的第一次出现.

关于如何解决这个问题的任何建议?有没有办法使用find_first_of,或者我应该简单地遍历char缓冲区的每个位置,并检查该位置的char是否与我的任何EOL字符匹配.

我所指的"find_first_of"函数就是这个:http://www.cplusplus.com/reference/algorithm/find_first_of/

任何帮助总是受到赞赏.

Rud*_*uis 5

find_first_of在这种情况下,函数返回一个指针,而不是索引,所以请尝试:

char *iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);
Run Code Online (Sandbox Code Playgroud)

  • 更常见的是,返回与前两个参数的类型相同. (2认同)