您的位置:首页 > 运维架构 > Linux

5——linux文件系统与设备文件系统

2010-06-06 08:51 387 查看
文件系统的相关调用:

int open(const char *pathname,int flags,mode_t mode)

路径名称,默认时认为在当前路径下面

int read(int fd,const void *buf,size_t length)读取fd文件length个字节
到所指的缓冲区buf中

int write(int fd,const void *buf,size_t legth) 读取缓冲区buf length个字节
到fd文件

int lseek(int fd,offset_t offset,int whence) 将文件读写指针相对whence移动offset个字节

返回文件指针相对于文件头的位置

SEEK_SET,相对文件开头

SEEK_CUR,相对文件读写指针的当前位置

SEEK_END;相对文件末尾

lseek(fd,0,SEEK_END) 返回值为文件相对于文件头的位置,返回值就是文件的长度

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#define LENGTH 100
main(void)
{
  int fd,len;
  char str[LENGTH];
  
  fd=open("hello.txt",O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
  if(fd)
    {
      write(fd,"hello world",strlen("hello world"));
      close(fd);
    }
  fd=open("hello.txt",O_RDWR);
  len=read(fd,str,LENGTH);
  str[len]='/0';
  printf("%s/n",str);
  close(fd);
}


在开发板上的显示如下 在文件夹下,原先没有hello.txt文件,通过open函数建立

[root@EmbedSky /xiangjie]# ls
4_book.ko  hello
[root@EmbedSky /xiangjie]# ls
4_book.ko  hello
[root@EmbedSky /xiangjie]# ./hello
hello world
[root@EmbedSky /xiangjie]# ls
4_book.ko  hello      hello.txt
[root@EmbedSky /xiangjie]# 
[root@EmbedSky /xiangjie]#






c库函数的文件操作

c库函数的文件操作实际上是独立于具体的操作系统平台的,不管是在DOS,windows,linux,VxWorks中,都是这些函数。

FILE *fopen()

int fgetc()

int fputc()

char *fgets().............
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: