Linux的学习之路:14、文件(1)

简介: Linux的学习之路:14、文件(1)

摘要

有一说一文件一天学不完,细节太多了,所以这里也没更新完,这里部分文件知识,然后C语言和os两种的文件操作


一、文件预备

1、文件=文件内容+属性(也就是数据)


2、文件的所有操作,无外乎是:对内容、对属性也就是文件的操作范畴


3、文件在磁盘(硬件)上放着,我们访问文件的流程:


先写代码->编译->exe->运行->访问文件。


文件的访问本质上是谁在访问文件呢?当然是进程了,我们写的代码就是进程,这个在之前的文章写了,这里就不具体解释了。


那么向硬件写入,只有谁有权利呢?这个肯定是OS也就是操作系统,那么普通用户也想写入呢?这个就是让OS提供接口,也就是各种语言封装了,要不然跨平台操作的时候需要把所有平台的代码都实现条件编译,动态裁剪,但是在不同语言下文件操作的接口都不一样,但是OS的接口只有一套。


4、显示器是硬件吗?printf像显示器打印和磁盘写入到文件没什么区别


5、在Linux的认为,一切皆文件


那么什么叫做文件呢?


站在系统的角度,能够被printf读取,或者能够被output写出的设备就叫做的文件。


侠义的文件:普通的磁盘文件


广义上的文件:显示器、键盘、网卡、声卡、显卡、磁盘、几乎所有的外设、都可以被称之为文件。

二、c文件操作


如下方代码就是利用C语言的函数进行创建文件并且三种写入文件的方式的演示,这里使用w进行写入,在文档中w是如果没有这个文件就会创建这个文件,也就是说我这里写入的是test1.txt这个文件,但是文件里没有就会进行生成一个文件。官方的使用代码选项如下,这里是利用man指令进行查看的。


r      Open text file for reading.  The stream is positioned at the beginning of the file.
       r+     Open for reading and writing.  The stream is positioned at the beginning of the file.
       w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.
       w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at the beginning of the file.
       a      Open for appending (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the end of the file.
       a+     Open  for  reading  and  appending  (writing at end of file).  The file is created if it does not exist.  The initial file position for reading is at the beginning of the file, but output is
              always appended to the end of the file.
 
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <unistd.h>
  5 
  6 int main()
  7 {
  8     FILE* fp=fopen("test1.txt","w");
  9     if(fp==NULL)
 10     {
 11         perror("fopen");
 12         return 1;
 13     }
 14     const char* s1="hello fwrite\n";
 15     fwrite(s1,strlen(s1),1,fp);
 16     const char* s2="hello fprintf\n";
 17     fprintf(fp,"%s",s2);
 18     const char* s3="hello fputs\n";
 19     fputs(s3,fp);
 20     fclose(fp);                                                                                                                                                                                               
 21     return 0;
 22 }
~

接着就是演示一下读取的示例,这里是读取一行进行打印在屏幕上。

6 int main()
  7 {
  8     FILE* fp=fopen("test1.txt","r");
  9     char buf[1024];
 10     const char *msg = "hello read!\n";                                                                                                                                                                        
 11     while(1)
 12     {
 13         ssize_t s = fread(buf, 1, strlen(msg), fp);
 14         if(s > 0)
 15         {
 16             buf[s] = 0;
 17             printf("%s\n", buf);
 18         }
 19         if(feof(fp))
 20         {
 21             break;
 22         }
 23 
 24     fclose(fp);
 25     return 0;
 26     }
 27 }

三、OS文件操作

1、系统文件I/O

操作文件,除了上述C接口(当然,C++也有接口,其他语言也有),我们还可以采用系统接口来进行文件访问,先来直接以代码的形式,实现和上面一模一样的代码,这里先是实现输出在文件里,如下方代码,这里是需要穿几个标志位,也就是放在一个int中的字节里,使用方式如下方代码,写文件。

#include <stdio.h>
 20 #include <sys/types.h>
 21 #include <sys/stat.h>
 22 #include <fcntl.h>
 23 #include <unistd.h>
 24 #include <string.h>
 25 int main()
 26 {
 27     umask(0);
 28     int fd = open("myfile", O_WRONLY|O_CREAT, 0644);                                   
 29     if(fd < 0){                                     
 30         perror("open");                 
 31         return 1;  
 32                                               
 33     }               
 34     int count = 5;     
 35     const char *msg = "hello word!\n";                                                                                                                                                                        
 36     int len = strlen(msg);
 37     while(count--){
 38         write(fd, msg, len);                                       
 39                                                                  
 40     }                                                     
 41     close(fd);                        
 42     return 0;                                         
 43                                               
 44 }      

读文件如下方代码所示

 #include <stdio.h>
 48 #include <sys/types.h>                                                                 
 49 #include <sys/stat.h>                               
 50 #include <fcntl.h>                      
 51 #include <unistd.h>
 52 #include <string.h>                           
 53 int main()          
 54 {                      
 55     int fd = open("myfile", O_RDONLY);  
 56     if(fd < 0)         
 57     {
 58         perror("open");                                            
 59         return 1;                                                
 60     }                                                     
 61     const char *msg = "hello word!\n";                                                                                                                                                                        
 62     char buf[1024];                                   
 63     while(1)                                  
 64     {                                                                                                             
 65         ssize_t s = read(fd, buf, strlen(msg));//类比write
 66         if(s > 0)
 67         {
 68             printf("%s", buf);
 69         }
 70         else
 71         {
 72             break;
 73         }
 74     }
 75     close(fd);
 76     return 0;
 77 }
~

2、接口介绍

这里是 利用man进行查看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);//函数接口的使用
pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。
参数:
O_RDONLY: 只读打开
O_WRONLY: 只写打开
O_RDWR  : 读,写打开
这三个常量,必须指定一个且只能指定一个
O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限
O_APPEND: 追加写

返回值:
成功:新打开的文件描述符
失败:-1

四、思维导图



目录
相关文章
|
16天前
|
Linux
linux中查看某个文件夹下文件的个数和大小
这篇文章介绍了在Linux系统中使用各种命令(如`stat`、`wc`、`du`和`ls`)来查看文件夹下文件的个数和大小的方法。
101 4
linux中查看某个文件夹下文件的个数和大小
|
2天前
|
移动开发 Linux
Linux 文件与目录管理
Linux 文件与目录管理
10 3
|
2天前
|
关系型数据库 MySQL Linux
Linux 文件基本属性
Linux 文件基本属性
10 3
|
1天前
|
Linux Shell
Linux系统文件默认权限
Linux系统文件默认权限
11 2
|
4天前
|
网络协议 Linux
linux学习之套接字通信
Linux中的套接字通信是网络编程的核心,允许多个进程通过网络交换数据。套接字提供跨网络通信能力,涵盖本地进程间通信及远程通信。主要基于TCP和UDP两种模型:TCP面向连接且可靠,适用于文件传输等高可靠性需求;UDP无连接且速度快,适合实时音视频通信等低延迟场景。通过创建、绑定、监听及读写操作,可以在Linux环境下轻松实现这两种通信模型。
9 1
|
11天前
|
安全 Linux 数据安全/隐私保护
探索Linux操作系统的文件权限管理
【9月更文挑战第29天】在数字世界中,文件权限管理如同保护我们隐私的锁。本文将带你了解如何在Linux系统中设置和管理文件权限,确保你的数据安全。我们将一起学习如何通过命令行工具来控制文件访问,就像学习一门新语言一样有趣。准备好了吗?让我们一起开启这场技术之旅!
|
13天前
|
Linux
深入理解Linux中的cp命令:文件与目录的复制利器
深入理解Linux中的cp命令:文件与目录的复制利器
|
13天前
|
Linux Shell
10-9|linux上统计文件中单词次数
10-9|linux上统计文件中单词次数
|
14天前
|
存储 Linux Shell
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
这篇文章介绍了Vim编辑器的基本使用、常用命令和模式,以及Linux系统中用户和组的管理方法,包括用户和组相关文件如/etc/passwd、/etc/shadow和/etc/group的说明。
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
|
1月前
|
Ubuntu Linux Shell
Linux系统命令 安装和文件相关命令
本文档详细介绍了Linux系统中的常用命令,包括软件安装卸载命令如`dpkg`和`apt-get`,压缩与解压命令如`gzip`、`bzip2`和`xz`,以及`tar`命令用于打包和解包。此外还介绍了文件分割命令`split`,文件操作命令如`cat`、`head`、`tail`、`more`、`less`等,管道命令和`wc`、`grep`、`find`、`cut`、`sort`、`uniq`、`diff`等实用工具。最后,文档还讲解了文件属性相关的命令如`chmod`、`chown`、`chgrp`以及创建硬链接和软链接的`ln`命令。