2

我有一个 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);

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

error: invalid conversion from ‘char*’ to ‘int’

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

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

我指的“find_first_of”函数是这个:http ://www.cplusplus.com/reference/algorithm/find_first_of/

任何帮助都将受到赞赏。

4

3 回答 3

5

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

char *iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);
于 2011-08-10T06:33:34.303 回答
1

我认为问题是这里的类型不匹配:

char * m_pLineBuff;

int iEOLChars[] = {'\n','\f','\r'};

尝试将您的声明iEOLCharschar数组。

于 2011-08-10T06:27:48.093 回答
-1

检查你的 first_first_of 函数我认为它永远不能有 4 个参数
参考first_first_of

于 2011-08-10T06:27:24.567 回答