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

时间:2011-08-10 06:21:30

标签: 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);

但是,我显然无法将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/

任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:5)

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

char *iEOLPosition = std::find_first_of(m_pLineBuff, m_pLineBuff+bytesToRead, iEOLChars, iEOLChars+3);

答案 1 :(得分:1)

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

char * m_pLineBuff;

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

尝试将iEOLChars声明为char数组。

答案 2 :(得分:-1)

检查你的first_first_of函数我认为它永远不会有4个参数
请参阅first_first_of