c - Searching for a combination of characters in a file -


i trying create program reads file , searches specific combination of characters. example: "/start/ 4jy42jygsfsf /end/".

so want find "strings" starting /start/ , ending /end/.

in order that, use read() function because file might binary file (it doesn't have file chars).

i call read() function that:

#define buffsize 4000  // more declarations  while (read(file_descriptor, buffer, buffsize) > 0) {     //search /start/     //then search /end/     //build string chars between these 2     //keep searching till reach end of buffer } 

assume every /start/ followed /end/.

the question is:

how deal cases combination of characters cut in half?

for example, let's first time read() gets called, in end of buffer spot /star , next time read() gets called @ start of second buffer there t/ 4jy42jygsfsf /end/.

this combination might cut anywhere. solutions thought result many many lines of code. there smart way deal these cases?

when reach end of buffer, record state of current partial match, if any. when next buffer, have 4 general cases:

  • not inside text matched.
  • saw beginning / @ end of last buffer
  • currently inside /start/. variable records how far have matched.
  • currently inside /end/. same variable /start/ records how far have matched.

your states inside matcher generally:

  1. currently not matching anything
  2. just saw / - next looking 's' or 'e'.
  3. matching either start/ or end/.
  4. matched - either /start or /end.

based on partial match, jump right state in matcher.

or

you can use pcre library. supports partial matching. overkill purposes.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -