Linux系统文件编程(一)

  • 在windows中如何编写一个文件呢?

步骤:打开/新建文档---->编辑文档---->保存文档---->关闭文档。

  • 那在Linux中如何编写一个文件呢?

骤:打开(open)---->读写(read/write)---->关闭(close)

  • 在Linux系统中的文件编程如下

  1. 打开/创建文件:open()函数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
 
const char *pathname  指打开文件的路径;
int flags             指打开文件的模式;
打开文件的模式有三种,如下:
    O_RDONLY(可读)  
    O_WRONLY(可写) 
    O_RDWR(可读可写)

open的返回值是一个文件描述符,是一个非负整数

什么是文件描述符:对于内核而言,所有打开文件都由文件描述符引用。文件描述符是一个非负整数。当打开一个现存文件或者创建一个新文件时,内核向进程返回一个文件描述符

open函数打开文件,打开成功返回一个文件描述符,打开失败,则返回-1

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(){
 
        int fd;//定义一个文件描述符
        fd = open("./file1",O_RDWR);//打开一个文件,然后返回一个文件描述符
        printf("fd = %d\n",fd);//打印文件描述符,要是本地中有file1文件,则返回3,否则返回-1
 
        return 0;
}

在文件有三种打开模式后,还能在后面添加几种模式

O_CREAT:若文件不存在则创建它

O_EXCL:如果同时指定了OCREAT,而文件已经存在,则返回-1

O_APPEND:每次写时都加到文件的尾端

O_TRUNC:去打开文件时,如果原文件中有内容,则把原文件中的内容清零,再写入新文件

  • 举例:O_CREAT:若文件不存在则创建它

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(){
 
        int fd;
        fd = open("./file1",O_RDWR);
        if(fd == -1){
 
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create file1 success!\n");
                }
        }
        return 0;
}
#O_CREAT 指若文件不存在,则创建它
#0600    指新创建文件的权限,可读可写

2.创建文件:creat()函数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int creat(const char *pathname, mode_t mode);
 
const char *pathname  指要创建的文件路径(包括文件路径和文件名)
mode_t mode           指要创建文件的权限
 
S_IRWXU  可读可写可执行
S_IRUSR  可读
S_IWUSR  可写
S_IXUSR  可执行
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
   
 int main()
 {
  
          int fd;
          //int creat(const char *pathname, mode_t mode);
          fd = creat("/home/hsj/file1",S_IRWXU);
          if(fd > 0)
          {
  
                  printf("creat succeed\n");
          }
  
         return 0;
 }

3、文件写入操作:write()函数

#include <unistd.h>
 
ssize_t write(int fd, const void *buf, size_t count);
 
int fd          文件描述符
const void *buf 是一个文件缓冲区,把这个文件写入到fd中
size_t count    要写入文件的大小
 
可以简单理解为:将缓冲区中的数据,写count个字节,写到fd中
写入成功的话,则返回写入字节大小,没有内容写入则返回0,错误写入则返回-1

4、文件读取操作:read()函数

#include <unistd.h>
 
ssize_t read(int fd, void *buf, size_t count);
 
int fd         要读取的文件
void *buf      读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
size_t count   请求读取的字节数
返回值:正确读取则返回读取到内容的字节大小,没有内容则返回0,错误读取则返回-1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{

  int fd;
  char *bud = "the is handsone boy";
  int len = strlen(bud);
  int  w_write;

 // int open(const char *pathname, int flags);
 fd = open("./file",O_RDWR|O_CREAT,0600);
 if(fd > 0)
  {
      printf("creat succeed\n");
  }

// ssize_t write(int fd, const void *buf, size_t count);
  w_write = write(fd,bud,len);

if(w_write > 0)
{
   printf("write succeed  write  %d  byte \n",w_write);
}

  lseek(fd,0,SEEK_SET);

  //ssize_t read(int fd, void *buf, size_t count);
  char *readbuf;
  readbuf =  (char *)malloc(sizeof(char)*w_write);

  int n_read = read(fd,readbuf,len);
  if(n_read > 0)
  {

   printf("read %d byte succeed, content: %s\n",n_read,readbuf);

  }

 close(fd);           
 return 0;
}

5、关闭文件操作:close()函数

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(){
 
        int fd;
        fd = open("./file1",O_RDWR);
        printf("fd = %d\n",fd);
 
        close(fd);//关闭文件
        return 0;
}

6、sizeof与strlen有以下区别:

(1)sizeof是一个操作符,而strlen是库函数。
(2)sizeof的参数可以是数据的类型,也可以是变量,而strlen只能以结尾为'\0'的字符串作参数。
(3)编译器在编译时就计算出了sizeof的结果,而strlen必须在运行时才能计算出来。
(4)sizeof计算数据类型占内存的大小(计算指针的长度),strlen计算字符串实际长度。
使用小技巧:遇见要分配内存的用sizeof,遇见要输出字符串长度的用strlen。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值