I/O操作

来源:互联网 发布:银行卡校验规则 js 编辑:程序博客网 时间:2024/05/16 06:21

内存分配

静态和全局变量程序启动时自动静态分配内存,函数参数和变量在构建时自动分配,退出时自动释放,当内存不固定时会动态分配
使用malloc动态分配内存

/* Include standard C library header. */#include< stdlib.h>.../* 分配一个16长度的int指针数组 */int* dynamicIntArray = (int*) malloc(sizeof(int) * 16);if (NULL == dynamicIntArray) {/* 内存不够分配 */...} else {/* Use the memory through the integer pointer. */*dynamicIntArray = 0;dynamicIntArray[8] = 8;.../* 释放内存. */free(dynamicIntArray);dynamicIntArray = NULL;}

sizeof获取数据类型需要的内存大小
动态修改分配好的内存

int* newDynamicIntArray = (int*) realloc(dynamicIntArray, sizeof(int) * 32);if (NULL == newDynamicIntArray) {/* Unable to reallocate enough memory. */...} else {/* Update the memory pointer. */dynamicIntArray = newDynamicIntArray;...

C++中动态分配内存,指定数据类型

int* dynamicInt = new int;if (NULL == dynamicInt) {/* Unable to allocate enough memory. */...} else {/* Use the allocated memory. */*dynamicInt = 0;...}delete dynamicInt;dynamicInt = 0;
int* dynamicIntArray = new int[16];if (NULL == dynamicIntArray) {/* Unable to allocate enough memory. */...} else {/* Use the allocated memory. */dynamicIntArray[8] = 8;...}

delete[] dynamicIntArray;
dynamicIntArray = 0;

标准I/O

Low-level I/O 精细的数据处理
Stream I/O 缓存I/O控制,用于处理流
stdin标准输入:应用程序的标准输入流
stdout标准输出:应用程序的标准输出流
stderr标准错误:应用程序标准错误流

打开一个文件流

#include<stdio.h>...FILE* stream = fopen("/data/data/com.example.hellojni/test.txt", "w");if (NULL == stream){/* File could not be opened for writing. */}else{/* Use the stream. *//* Close the stream. */}

FILE* fopen(const char* filename, const char* opentype);第二个参数opentype
r:打开现有文件为只读。
w:打开文件只写。如果该文件已经存在,它被截断至零长度。
a:打开追加模式的文件。文件内容被保存下来,并附加到文件的末尾。如果该文件不存在,则打开一个新的文件
r+:打开在读写模式下的文件。
w+:打开在读写模式下的文件。如果该文件已经存在,它被截断零长度。
a+:打开文件进行读取和追加。初始文件位置被设置为开始用于读取和向用于附加该文件的结束。

写入流

size_t fwrite(const void* data, size_t size, size_t count, FILE* stream);
从流stream中读取count个数的char写入
如成功则返回count的个数

写入字符串到流中,写入失败返回EOF

/* Writing character sequence to stream. */if (EOF == fputs("hello\n", stream)){/* Error occured while writing to the stream. */}

写入单个字符,写入成功返回字符本身,否则返回EOF

char c = 'c';/* Writing a single character to stream. */if (c ! = fputc(c, stream)){/* Error occured while writing character to string.}

写入格式化数据到流
int fprintf(FILE* stream, const char* format, …);

  %d%i:格式化整数参数作为符号的十进制。%u:格式化无符号整数为无符号小数。%o:格式化的无符号整数参数为八进制。%x:格式化的无符号整数参数为十六进制。%c:格式化整数参数为单个字符。%f:格式化双精度参数为浮点数。%e:格式固定格式的双精度参数。%s:打印指定的空值终止的数组。%p:打印给定指针的内存地址。%%:写一个%字符。

例如

/* Writes the formatted data. */if (0>fprintf(stream, "The %s is %d.", "number", 2)){/* Error occurred while writing formatted data. */}

成功返回写入流字符串,当应用程序的正常终止。当一个换行符是写在行缓冲的情况下。当缓冲区已满。
当流被关闭。下会失败
调用int fflush(FILE* stream);刷新缓冲区

char data[] = { 'h', 'e', 'l', 'l', 'o', '\n' };size_t count = sizeof(data) / sizeof(data[0]);/* Write data to stream. */fwrite(data, sizeof(char), count, stream);/* Flush the output buffer. */if (EOF == fflush(stream)){/* Error occured while flushing the buffer. */}

fputs(“abcd”,stream);写入abcd到流中

读取流

size_t fread(void* data, size_t size, size_t count, FILE* stream);
读取字符,从stream中读取4位char到buffer中,成功则返回count

char buffer[5];size_t count = 4;/* Read 4 characters from the stream. */if (count ! = fread(buffer, sizeof(char), count, stream)){/* Error occured while reading from the stream. */}else{/* Null terminate. */buffer[4] = NULL;/* Output buffer. */MY_LOG_INFO("read: %s", buffer);}

读取字符串,读取最多1024个字符到buffer中

char buffer[1024];/* Read newline terminated character sequence from the stream. */if (NULL == fgets(buffer, 1024, stream)){/* Error occured while reading the stream. */}else{MY_LOG_INFO("read: %s", buffer);

fgetc方法可读取一个简单无符号类型字符

unsigned char ch;int result;/* Read a single character from the stream. */result = fgetc(stream);if (EOF == result){/* Error occured while reading from the stream. */}else{/* Get the actual character. */ch = (unsigned char) result;}

读取格式化字符 int fscanf(FILE* stream, const char* format, …);

char s[5];int i;/* Stream has "The number is 2" *//* Reads the formatted data. */if (2 != fscanf(stream, "The %s is %d", s, &i)){/* Error occured while reading formatted data. */}

成功返回读取的个数,失败返回EOF,s为number i为2

判断是否到文件流结尾int feof(FILE* stream);到达结尾则返回一个非0值

char buffer[1024];/* Until the end of the file. */while (0 == feof(stream)){/* Read and output string. */fgets(buffer, 1024, stream);MY_LOG_INFO("read: %s", buffer);

指针流偏移fseek函数 fseek(stream, -4, SEEK_CUR);
SEEK_SET:偏移量是相对于流的开头。
SEEK_CUR:偏移量是相对于当前位置。
SEEK_END:偏移是相对于流的末尾。

先写入abcd,然后从当前位置向前偏移4位覆盖写入efgh

/* Write to the stream. */fputs("abcd", stream);/* Rewind for 4 bytes. */fseek(stream, -4, SEEK_CUR);/* Overwrite abcd with efgh. */fputs("efgh", stream);

返回0则成功,否则返回非零

检查流是否在之前发生过错误,如有则返回非零值

if (0 != ferror(stream)){/* Error occured on the previous request. */}

关闭流

if (0 != fclose(stream)){/* Error occured while closing the stream. */}

执行shell命令

include<stdlib.h>int result;= system("mkdir /data/data/com.example.hellojni/temp");

打开一个流指针
FILE popen(const char command, const char* type);
关闭 pclose

#include<stdio.h>...FILE* stream;/* Opening a read-only channel to ls command. */stream = popen("ls", "r");if (NULL == stream){MY_LOG_ERROR("Unable to execute the command.");}else{char buffer[1024];int status;/* Read each line from command output. */while (NULL ! = fgets(buffer, 1024, stream)){MY_LOG_INFO("read: %s", buffer);}/* Close the channel and get the status. */status = pclose(stream);MY_LOG_INFO("process exited with status %d", status);}

获取系统属性

int __system_property_get(const char* name, char* value);
通过名字获取一个属性,ro.product.model为当前系统属性,获取值为如HM-2A

include<sys/system_properties.h>char value[PROP_VALUE_MAX];/* Gets the product model system property. */if (0 == __system_property_get("ro.product.model", value)){/* System property is not found or it has an empty value. */} {MY_LOG_INFO("product model: %s", value);

找一个系统值__system_property_find

const prop_info* property;/* Gets the product model system property. */property = __system_property_find("ro.product.model");if (NULL == property){/* System property is not found. */}else{char name[PROP_NAME_MAX];char value[PROP_VALUE_MAX];/* Get the system property name and value. */if (0 == __system_property_read(property, name, value)){MY_LOG_INFO("%s is empty.");}else{MY_LOG_INFO("%s: %s", name, value);}}

__system_property_read函数为读取值,name数组为可选参数,如果有,则读取的值不大于name数组长度,value为读取返回的值,读取的值不大于value数组的长度

用户和组

uid_t uid;/* Get the application user ID. */uid = getuid();MY_LOG_INFO("Application User ID is %u", uid);char* username;/* Get the application user name. */username = getlogin();MY_LOG_INFO("Application user name is %s", username);
0 0