打开文件读
int fd = open(pcm_file, O_RDONLY, 0)
读文件
static ssize_t safe_read(int fd, void *buf, size_t count) {
ssize_t result = 0, res;
while (count > 0) {
if ((res = read(fd, buf, count)) == 0)
break;
if (res < 0)
return result > 0 ? result : res;
count -= res;
result += res;
buf = (char *)buf + res;
}
return result;
}
打开文件写
int fd = open(pcm_file, O_WRONLY | O_CREAT, 0644)
写文件
static ssize_t xwrite(int fd, const void *buf, size_t count)
{
ssize_t written;
size_t offset = 0;
while (offset < count) {
written = write(fd, buf + offset, count - offset);
if (written <= 0)
return written;
offset += written;
};
return offset;
}