Newer
Older
Skill_semi_sample / SS2301-03 / ss2301-03.cpp
#include <iostream>
#include <fstream>
#include <vector>

bool is_binary(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    if (!file) {
        std::cerr << "Cannot open file: " << filename << std::endl;
        return false;
    }

    char ch;
    while (file.get(ch)) {
        // ヌルバイトが含まれている場合はバイナリとみなす
        if (ch == 0x00) {
            return true;
        }
    }

    return false;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <file1> <file2> ..." << std::endl;
        return 1;
    }

    for (int i = 1; i < argc; ++i) {
        const std::string filename = argv[i];
        if (is_binary(filename)) {
            std::cout << filename << ": Binary file" << std::endl;
        } else {
            std::cout << filename << ": Text file" << std::endl;
        }
    }

    return 0;
}