内容包括:
open() ,尤其是各种常见的参数,到底是什么意思, 比如常用创建一个空文件: fd = open("/tmp/xx。txt",O_RDWR | O_CREAT | O_TRUNC);
读一个文件,fd = open("/dev/hello",O_RDONLY );
如果就是要写一个既有的文件,fd = open("/dev/hello",O_WRONLY );
具体的参数: man 2 open
read(),
write()
ioctl() ,这个最重要。
lseek() , 文件指针移动
比如下面的例子:
就是简单的读一个文件:
#include
#include
#include
#include
int main(int argc,char **argv)
{
int fd = 0;
int pid = 0;
char buffer[20] = {'\0'};
char *read_buffer[20] = {'\0'};
//fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);
fd = open("/dev/hello",O_RDONLY ); //| O_NONBLOCK);
printf("fd=%d\n",fd);
if(fd
#include
#include
#include
int main(int argc,char **argv)
{
int fd = 0;
int pid = 0;
char buffer[20] = {'\0'};
char write_buffer[20] = {'\0'};
strcpy(write_buffer,"zhanglinbao");
fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);
//fd = open("/dev/hello",O_RDONLY);
printf("fd=%d\n",fd);
if(fd < 0) {
perror("/dev/hello");
return -1;
}
write(fd,write_buffer,sizeof(write_buffer)-1);
close(fd);
return 0;
}。
全部