Trying to read binary file as text but scanner stops at first line

后端 未结 3 1601
野的像风
野的像风 2021-01-27 02:56

I\'m trying to read a binary file but my program just stops at first line.. I think it\'s because of the strange characters the file has..I just want to extract some directions

3条回答
  •  时光取名叫无心
    2021-01-27 03:36

    You cannot use a line-oriented scanner to read binary files. You have no guarantee that the binary file even has "lines" delimited by newline characters. For example, what would your scanner do if there were TWO files matching the pattern "D:\.*.mp3" with no intervening newline? You would extract everything between the first "D:\" and the last ".mp3", with all the garbage in between. Extracting file names from a non-delimited stream such as this requires a different strategy.

    If i were writing this I'd use a relatively simple finite-state recognizer that processes characters one at a time. When it encounters a "d" it starts saving characters, checking each character to ensure that it matches the required pattern, ending when it sees the "3" in ".mp3". If at any point it detects a character that doesn't fit, it resets and continues looking.

    EDIT: If the files to be processed are small (less than 50mb or so) you could load the entire file into memory, which would make scanning simpler.

提交回复
热议问题