Linux文件操作1

3/23
操作系统的五大功能模块是:处理器管理、存储器管理、设备管理、文件管理和作业管理。
文件编程 进程线程编程 TCP/UDP
Linux文件:文件本身包含的数据(打开文件可以看到文件信息)

2.文件属性(元数据:文件的访问权限,文件大小,创建时间等)
查看文件属性 ls -l
目录也是文件之一(当前)

. :当前目录
…:上一级目录

/var:
Cache:应用程序的缓存文件
Lib:应用程序的信息、数据
Local:usr/local中程序的信息
Lock:锁文件
Log:日志文件
Opt:/opt中程序信息
Run:正在执行的程序信息,PID文件
Tmp:临时文件传递
Media:文件系统的挂载点

Linux文件类型
操纵系统:内核:操纵系统对于软硬件和系统资源分配的最重要的核心部分。
系统调用:操纵系统提供给用户的一组“特殊”的接口,用户可以通过这组数据接口来获取操纵系统内核提供的服务。

用户编程接口:系统调用并不是直接和查询员进行交互,它仅仅是一个通过软件中断机制向内核提出请求,以获取内核服务的接口,在实际使用中程序员调用的通常是用户编程接口(API)

系统命令相对于API来说是更高层级

文件描述符:本质是一个正整数 open函数0–open–MAX
man 1 open

缓冲区:程序中每个文件都可以使用
磁盘文件–>缓冲区—>内核(cpu)

open 系列函数:create:创建文件函数
Int creat (文件描述符,创建模式)
S_IRUSR 可读
S_IWUSR 可写
S_XUSR 可执行
S_IXEWU 可读,可写,可执行

1.c文件

int main()
{
    int  fd = creat("./test", S_IRWXU);
    if(-1 == fd ) //if ( fd == -1)
    {
	printf("creat error !\n");
	return 1;
    }
    printf("fd=%d\n",fd);
    return 0;
}

Open(“文件名”,flag:打开方式,mode如果没有创建那么自动略过);
O_RDONLY:以只读方式打开文件
O_WRONLY:以只写方式打开文件
O_RDWR:以可读可写的方式打开
O_CREAT:如果文件存在就打开
如果不存在就创建,出错返回值-1;
O_APPEND:写文件的时候追加在文件末尾

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>

int main()
{
    int fd = open("./1.c",O_RDWR);
    if(-1 == fd)
    {
	printf("error!\n");
	return 1;
    }
    printf("fd=%d\n",fd);
    return 0;
}

Write函数:向文件写入数据
Write(文件描述符,写入的数据指针,写入的数据内存大小)

#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    int fd = open("./test1.txt",O_RDWR|O_CREAT,0700);
    if(-1 == fd)
    {
	perror("open!");
	return 1;
    }
    printf("fd=%d\n",fd);
    
    char buff[1024] = "helloworld!  this is the first time write file!";
	write(fd,buff,strlen(buff));

    return 0;
}

Read 从文件读取数据
Read(文件描述符,读入某个变量,读出文件大小)

lseek调整光标的位置
lseek(文件描述符,光标移到的位置数,光标移动的形式)
SEEK_SET:将光标移动到开头再增加相应的offset位置
SEEK_CUR:将光标移动到文件的当前位置再往后加offset的位置
SEEK_END:将光标移动到文件的末尾再增加offset的位置
lsee函数返回值:返回值从文件开头到光标位置有多少个字符
Int length = lseek(fd,0,SEEK_END);
Printf(“”);

#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
      int fd = open("./text1.txt",O_RDWR|O_CREAT,0700);
      if(-1 == fd)
      {
	printf("open");
	return 1;
      }
      printf("fd=%d\n",fd);

      char buff[1024] = "helloworld";
      write(fd,buff,strlen(buff));
      close(fd);

      char buffer[1024] = {0};
      int fd1 = open("./text1.txt",O_RDWR|O_CREAT,0700);
      if(-1 == fd1)
      {
	  perror("open!");
	  return 1;

      }
      read(fd1,buffer,strlen(buff));
printf("%s\n",buffer);

close(fd);
return 0;
}

修改后

**

#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
      int fd = open("./text2.txt",O_RDWR|O_CREAT,0700);
      if(-1 == fd)
      {
	  printf("open");
	  return 1;
      }
      printf("fd=%d\n",fd);
      char buff[1024] = "helloworld  this is test2";
      write(fd,buff,strlen(buff));
   
      char buffer[1024] = {0};
      lseek(fd,0,SEEK_SET);
      read(fd,buffer,strlen(buff));
      printf("%s\n",buffer);
      close(fd);
      return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺尸研究员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值