linux C 读文件

来源:互联网 发布:卡秋莎软件 编辑:程序博客网 时间:2024/06/11 09:44

之前看书有点印象,然后匆忙之间被叫读取一下 /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 8192std::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;}

openfopen 是不同的 。
fopen 是走用户态库函数的调用,open 是走系统调用。

原创粉丝点击