How to identify the file content as ASCII or binary

后端 未结 10 2584
旧巷少年郎
旧巷少年郎 2020-11-28 05:54

How do you identify the file content as being in ASCII or binary using C++?

10条回答
  •  一个人的身影
    2020-11-28 06:22

    To check, you must open the file as binary. You can't open the file as text. ASCII is effectively a subset of binary. After that, you must check the byte values. ASCII has byte values 0-127, but 0-31 are control characters. TAB, CR and LF are the only common control characters. You can't (portably) use 'A' and 'Z'; there's no guarantee those are in ASCII (!). If you need them, you'll have to define.

    const unsigned char ASCII_A = 0x41; // NOT 'A'
    const unsigned char ASCII_Z = ASCII_A + 25;
    

提交回复
热议问题