【发布时间】:2015-04-22 23:28:32
【问题描述】:
我正在尝试创建一个程序来读取文件并搜索特定的字符组合。 例如:“/start/4jy42jygsfsf /end/”。
所以我想找到所有以 /start/ 开头并以 /end/ 结尾的“字符串”。
为了做到这一点,我使用了 read() 函数,因为文件可能是二进制文件(它不必是带有字符的文件)。
我这样调用 read() 函数:
#define BUFFSIZE 4000
// more declarations
while (read(file_descriptor, buffer, BUFFSIZE) > 0)
{
//search for /start/
//then search for /end/
//build a string with all the chars between these two
//keep searching till you reach the end of buffer
}
假设每个 /start/ 后跟一个 /end/。
问题是:
这种字符组合被切成两半的情况如何处理?
例如,假设第一次调用 read(),在这个缓冲区的末尾我发现 /star,而下一次 read() 在第二个开始时被调用缓冲区有 t/ 4jy42jygsfsf /end/。
这种组合可能会在任何地方被削减。我认为的解决方案会产生很多行代码。有什么聪明的方法来处理所有这些情况吗?
【问题讨论】: