linux 基础IO

open函数

//功能:打开文件
int open(const char *path, int flags );
参数:
path:要打开的文件
flags:打开方式
O_RDONLY : 只读方式打开
O_WRONLY :只写方式打开
O_RDWR :读写方式打开
O_TRUNC :清空文件
O_APPEND :追加
返回值:
失败:-1
成功:文件描述符
//创建文件
int open(const char *path, int flags, mode_t mode);
path: 要创建的文件名
flags:O_CREAT

read函数

//功能: 从fd文件中读取数据到buf所指向的空间,该空间大小为len
//返回值为:实际读取的字节数
int read(int fd, char *buf, size_t len);

write函数

//功能:往fd所指的文件中写入数据,数据的起始位置为buf,大小为len
int write(int fd, const char *buf, size_t len);

close函数

int close(int fd);
fd表示文件标识符
返回值:返回0表示成功,返回-1表示失败

代码

write :

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

int main()
{         
    umask(0);
    int fd=open("myfile1",O_WRONLY|O_CREAT,0644);//open函数,0644代表权限
    if(fd<0){
        perror("open");
        return 1;
    }
    int count=5;
    const char*msg="hello world\n";
    int len=strlen(msg);
    while(count--){
        write(fd,msg,len+1);
    }
    close(fd);
    return 0;
}                                                      

read:

#include <stdio.h>                                                                   
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main()
{
    int fd;
    if( (fd=open("mmc",O_RDWR)) == -1)
        perror("open"),exit(1);
    char buf[4] = {};
 while(1)
    {
        memset(buf,0x00,sizeof(buf));
        int r = read(fd,buf,3);
        if(r<=0)
        {
            break;
        }
        printf("%s",buf);
        fflush(stdout);
        sleep(1);
    }
    close(fd);
}                        

文件描述符fd

1)概念:问价描述符是一个非常小的非负整数,内核用以标识一个特定进程正在访问的文件。当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符。
2)3个缺省的文件描述符:
(1)linux进程默认情况下有3个缺省打开的文件描述符,它们分别是标准输入0,标准输出1,标准错误2.
(2)0,1,2对应的物理设备一一般是:键盘,显示器,显示器。
注意:POSIX定义了STDIN_FILENO,STDOUT_FILENO和STDERR_FILENO来代替0,1,2.

FILE结构体的内容

struct file结构体定义在/linux/include/linux/fs.h中,文件结构体代表一个打开的文件,系统中每个打开的文件在内核空间都有一个关联的struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数,在文件的所以实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码中,struct file的指针通常被命名为file或filp。

fd属于系统库 , FILE属于c标准库;
fd是一个整数,FILE是一个结构体.

myshell实现

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

int main()
{         
    close(1);
    int fd=open("myfile1",O_WRONLY|O_CREAT,0644);//open函数,0644代表权限
    if(fd<0){
        perror("open");
        return 1;
    }

    printf("fd:%d",fd);
    fflush(stdout);
    close(fd);
    exit(0);
    return 0;
}                         

静态库

静态库:对一系列.o文件打包
生成方法:
ar -cr lib库名.a 文件
libmymath.a : 库文件名
mymath :库名
使用:
gcc main.c -L. -l库名
寻找库:
/lib64
/usr/lib64
-L 库路经

动态库

动态库(共享库 shared library)
生成动态库
gcc -fPIC -shared libxxx.so xxx.c xx.c
链接动态库
gcc main.c -L. -lxxx
使用动态库
1、/lib64/
/usr/lib64
2、export LD_LIBRARY_PATH=.
3、/etc/ld.conf.d/xxx.conf 文件中将动态库路径写入

代码

add.h

#ifndef __ADD_H__                                                               
#define __ADD_H__

int add(int a,int b); 

#endif /// __ADD_H__

add.c

#include "add.h"                                                                

int add(int a,int b)
{
    return a + b;
}

sub.h

#ifndef __SUB_H__                                                               
#define __SUB_H__

int sub(int a,int b); 

#endif /// __SUB_H

sub.c


#include "sub.h"                                                                #incl 

int sub(int a,int b)
{
    return a - b;
}

mul.h

#ifndef __MUL_H__                                                               
#define __MUL_H__

int mul(int a,int b); 

#endif /// __MUL_H                       

mul.c

#include "mul.h"                                                                

int mul(int a,int b)
{
    return a * b;
}

div.h

#ifndef __DIV_H__                                                               
#define __DIV_H__

int div(int a,int b); 

#endif /// __DIV_H

div.c

#include "div.h"                                                                

int div(int a,int b)
{
    return a/b;
}

main.c

#include <stdio.h>

#include "add.h"                                                                
#include "sub.h"
#include "mul.h"
#include "div.h"

int main()
{
    int a=20;
    int b=10;
    printf(" %d + %d  = %d\n",a,b,add(a,b));
    printf(" %d - %d  = %d\n",a,b,sub(a,b));
    printf(" %d * %d  = %d\n",a,b,mul(a,b));
    printf(" %d / %d  = %d\n",a,b,div(a,b));
}

makefile

.PHONY:libmymath.a clean

libmymath.a:main.o add.o sub.o mul.o div.o 
        ar -rc $@ $^                                                                 
%.o : %.c 
        gcc -c $^ -o $@
clean:
        rm -rf *.o

静态库实现

这里写图片描述

动态库实现

直接./a.out有错误,更改一下 LD_LIBRARY_PATH
(方法:export LD_LIBRARY_PATH=.)
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值