之前看书有点印象,然后匆忙之间被叫读取一下 /proc/cpuinfo 没能立刻写出来 : (,做个笔记吧。
#include <jni.h> #include <string> #include <sys/utsname.h> #include <sys/system_properties.h> #include <cstdlib> #include <fcntl.h> #include <unistd.h> #define BUF_SIZE 8192 std::string read_file(char *name) { std::string file_string = ""; int input_fd; ssize_t ret_in; char buffer[BUF_SIZE]; input_fd = open(name, O_RDONLY); if (input_fd == -1) { perror("open"); file_string += "error"; } else { while ((ret_in = read(input_fd, &buffer, BUF_SIZE)) > 0) { file_string += buffer; } } close(input_fd); return file_string; }open 和 fopen 是不同的 。 fopen 是走用户态库函数的调用,open 是走系统调用。