Linux---文件操作

Posted qnbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux---文件操作相关的知识,希望对你有一定的参考价值。

C语言中的文件读&写

#include <stdio.h>

int main()
{
  FILE* fp = fopen("log.txt","r");
  if(fp == NULL)
  {
    perror("fopen");
    return 1;
  }
  int ct = 5;
  char buf[64];
  while(ct){
  
    fgets(buf,sizeof(buf),fp);
    printf(buf);
    ct--;
  }
  /*
  int ct = 5;
  while(ct)
  {
    fputs("hello world \\n",fp);
    ct--;
  }
  */
  fclose(fp);
  return 0;
}

stdin

标准输入

FILE* fp = fopen("log.txt","r");
  if(fp == NULL)
  {
    perror("fopen");
    return 1;
  }
  int ct = 5;
  char buf[64];
  while(ct){
  
    fgets(buf,sizeof(buf),stdin);
    printf(buf);
    ct--;
  }

stdout

标准输出

int ct = 5;
  while(ct)
  {
    fputs("hello world \\n",stdout);
    ct--;
  }

stderr

标准错误

int ct = 5;
  while(ct)
  {
    fputs("hello world \\n",stderr);
    ct--;
  }

a–追加写入

#include <stdio.h>

int main()
{
  FILE* fp = fopen("log.txt","a");//append
  if(fp == NULL)
  {
    perror("fopen");
    return 1;
  }
  int ct = 5;
  while(ct)
  {
    fputs("hello L \\n",fp);
    ct--;
  }
  
  fclose(fp);
  return 0;
}

write-- 覆盖写入

#include <stdio.h>

int main()
{
  FILE* fp = fopen("log.txt","w");
  if(fp == NULL)
  {
    perror("fopen");
    return 1;
  }
  int ct = 5;
  while(ct)
  {
    fputs("welcome!\\n",fp);
    ct--;
  }
  
  fclose(fp);
  return 0;
}

系统中的I/O

接口

  • O_RDONLY 只读打开
  • O_WRONLY只写打开
  • O_RDWR读写打开
  • O_CREAT若文件不存在,则创建
  • O_APPEND追加写
  • 打开成功返回值 大于0
  • 打开失败返回1

文件描述符

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

int main()
{
  int fd = open("log.txt",O_WRONLY,0666);
  printf("fd: %d\\n",fd);

  return 0;
}

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

int main()
{
  int fd = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",fd);

  return 0;
}


找标志位


open

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

int main()
{
  int f1 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f1);
  int f2 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f2);
  int f3 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f3);
  int f4 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f4);
  int f5 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f5);

  return 0;
}

write&close

const char* msg = "welcome L\\n";
  write(1,msg,strlen(msg));

char buff[32];
  read(0,buff,32);
  printf("%s\\n",buff);

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
  int f1 = open("log.txt",O_WRONLY|O_CREAT,0666);
  if(f1 < 0){
    
    printf("open error!\\n");
  }
  printf("fd: %d\\n",f1);
  
  int ct = 5;
  const char* msg = "hello world!\\n";
  while(ct)
  {
    write(f1,msg,strlen(msg));//strlen不需要+1
    ct--;
  }

  close(f1);
  return 0;
}

read

#include <string.h>
int main()
{
  int f1 = open("log.txt",O_RDONLY);
  if(f1 < 0){
    
    printf("open error!\\n");
  }
  printf("fd: %d\\n",f1);
  
  char c;
  while(1)
  {
    ssize_t s = read(f1,&c,1);
    if(s <= 0)
    {
      break;
    }
    write(1,&c,1);//fwrite(,,,stdout);
  }
  close(f1);

  return 0;
}

文件标识符分配规则

从最小但是没有被使用的开始分配


关闭显示器(1)

 close(1);
  int f2 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f2);
  int f3 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f3);
  int f4 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f4);
  int f5 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f5);

  return 0;


关0

 close(0);
  int f2 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f2);
  int f3 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f3);
  int f4 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f4);
  int f5 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f5);

  return 0;

 close(0);
 close(2);
  int f2 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f2);
  int f3 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f3);
  int f4 = open("log.txt",O_WRONLY|O_CREAT,0666);
   printf("fd: %d\\n",f4);
  int f5 = open("log.txt",O_WRONLY|O_CREAT,0666);
  printf("fd: %d\\n",f5);

  return 0;

int fd = open("log.txt",O_WRONLY|O_CREAT,0666);
  if(fd < 0){
    return 1;
  }
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);

  close(fd);

close(1);
int fd = open("log.txt",O_WRONLY|O_CREAT,0666);
  if(fd < 0){
    return 1;
  }
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);
  write(fd,"hello\\n",6);

  close(fd);

重定向

输出重定向

close(1);
int fd = open("log.txt",O_WRONLY|O_CREAT,0666);
  if(fd < 0){
    return 1;
  }
  write(1,"hello\\n",6);
  write(1,"hello\\n",6);
  write(1,"hello\\n",6);
  write(1,"hello\\n",6);
  write(1,"hello\\n",6);

  close(fd);

close(1);
  int fd = open("log.txt",O_WRONLY|O_CREAT,0666);            
   if(fd < 0){                                                
      return 1;                                                
    }                      

   printf("hello you:%d\\n",123);
   printf("hello you:%c\\n",'v');
   printf("hello you:%f\\n",3.335);
   fflush(stdout);

 close(1);
  int fd = open("log.txt",O_WRONLY|O_CREAT,0666);            
   if(fd < 0){                                                
      return 1;                                                
    }                      

   printf("hello",stdout);
   printf("hello",stdout);
   printf("hello",stdout);


  int fd = open("log.txt",O_WRONLY|O_CREAT,0666);            
   if(fd < 0){                                                
      return 1;                                                
    }                      

   printf("hello",stdout);
   printf("hello",stdout);
   printf("hello",stdout);


重定向本质是修改文件描述符下标对应的struct file 指向的内容*
struct FILE 是一个结构体,里面必定包含一个成员叫fd

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
  umask(0

以上是关于Linux---文件操作的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode 如何操作用户自定义代码片段(快捷键)

[linux][c/c++]代码片段01

[linux][c/c++]代码片段02

LINUX PID 1和SYSTEMD PID 0 是内核的一部分,主要用于内进换页,内核初始化的最后一步就是启动 init 进程。这个进程是系统的第一个进程,PID 为 1,又叫超级进程(代码片段