Linux学习笔记(三):函数、文件IO和线程

接到以上文章:
Linux学习笔记(一)

Linux学习笔记(二)

复习

一维数组和指针:int a[10];int *p=a;
在这里插入图片描述
二维数组和指针:int *q = &z[0[0]
在这里插入图片描述
指针数组int *p[n];===存多个地址的数组
数组指针int (*p)[n];===指向整个数组的指针
函数指针数组int (*p[n])();===存函数地址的数组
在这里插入图片描述

函数

函数:解决问题的方法
声明

函数类型 函数名(形式参数。。。){
		具体功能实现;
}

函数传参值传递、地址传递、全局变量(函数传参的过程是一个值拷贝的过程)
数组作为参数传递:以指针的方式传递

结构体

结构体: 程序员自己定义的数据类型
结构体定义:

1、只有结构体定义
struct stuff{  
      	 	char job[20];  
   		    int age;  
 			float height;  
};  
2、附加该结构体类型的“结构体变量”的初始化的结构体定义
struct stuff{  
      		char job[20];  
     		int age;  
     		float height;  
};  
struct stuff Huqinwei;
Eg:
struct student{
				int weight;
				int height;
				char name[10];
};
stuct student stu = {40,175,”wyj”};

更改数值用strcpy

用一条语句声明结构和该结构的实例时,可以省略标记符名字

结构体数组:

struct student stu[50] = {{30,176,”euwhj”},{46,185,”hjdu”}};
strcpy(stu[0].name,”wudh”);//字符型用strcpy更改
stu[0].weight = 40;//整型可直接赋值 

结构体指针:

struct student *student = stu;
printf(“weight:%d\nheight:%d\nname:%s\n”,student->weight,student->height,student->name);
结构体指针用指向取值(eg:student->weight)

在这里插入图片描述

文件I/O

Man手册查看函数的声明(q退出)

i.open(const char *pathname(被打开的文件名),int flags,mode t mode(打开文件的权限) );
看返回值:若返回值小于0,则打开失败;若返回值大于0,则打开成功。

ii.close(int fildes);// fildes(open的返回值)
看返回值:若返回值小于0,则打开失败;若返回值大于0,则打开成功。

iii.read(int fd,char *buf,size_t count);
调用成功返回读取的字节数

iv.write(int fd,const *buf,size_t count);//const:把变量常量化

v.lseek(int fd,off_t offset,int whence);

fd:文件描述符
offset:偏移量,每一读写操作所需要的距离
SEEK_SET:当前位置为文件的开头,新位置为偏移量的大小
SEET_END:当前位置为文件的结尾,新位置为文件的大小加上偏移量的大小
SEET_CUR:当前位置为文件指针的位置,新位置为当前位置加上偏移量

在这里插入图片描述

线程

线程: 程序的一次执行过程
pthread_create:创造线程
在这里插入图片描述
编译时要手动给一个链接:gcc 文件名 -lpthread
在这里插入图片描述
Pthread_join:阻塞线程
在这里插入图片描述
pthread_join()函数会一直阻塞调用线程,直到指定的线程tid终止。当pthread_join()返回之后,应用程序可回收与已终止线程关联的任何数据存储空间,(另外也可设置线程attr属性,当线程结束时直接回收资源)如果没有必要等待特定的线程。

代码演示

读写图片:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
    int fp;
    fp = open("1.jpg",O_RDWR);
    if(fp < 0){
        printf("open error\n");
        exit(EXIT_FAILURE);
    }
    printf("open success\n");

    int fd = open("2.jpg",O_RDWR);
    if(fp < 0){
        printf("open error\n");
        exit(EXIT_FAILURE);
     }
    printf("open success\n");

    while(1){
        char buf[64] = {0};
        int ret = read(fp,buf,64);
        if(ret < 0){
            printf("raed errorr\n");
            return -1;
        }
        if(ret == 0){
            break;
        }
        ret = write(fd,buf,64);
    }
    close(fp);
    close(fd);
return 0;
}

查看图片:eog 2.jpg

加密和解密:

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

int jm(){
    char path[30] = {0};
    gets(path);
    int f = open(path,O_RDWR);
    if(f < 0){
        perror("open");
        return -1;
    }
    int i;
    printf("1.加密  2.解密 :");
    scanf("%d",&i);
    char head;
    read(f,&head,1);
    if(i == 1){
        head = head + 1;
    }
    if(i == 2){
       head = head - 1; 
    }
    lseek(f,0,SEEK_SET);
    write(f,&head,1);
    close(f);
    return 0;
}

int main(){
    int fp;
    fp = open("1.jpg",O_RDWR);
    if(fp < 0){
        printf("open error\n");
        exit(EXIT_FAILURE);
    }
    printf("open success\n");

    int fd = open("2.jpg",O_RDWR);
    if(fp < 0){
        printf("open error\n");
        exit(EXIT_FAILURE);
     }
    printf("open success\n");

    while(1){
        char buf[64] = {0};
        int ret = read(fp,buf,64);
        if(ret < 0){
            printf("raed errorr\n");
            return -1;
        }
        if(ret == 0){
            break;
        }
        ret = write(fd,buf,64);
    }

    jm();
    close(fp);
    close(fd);
return 0;
}

收获及感悟

今天最先复习了昨天主要学习的内容,之后主要学习了结构体、文件I/O和线程三大类,在巩固以前学过的知识的同时也学习了很多新的东西。文件I/O和线程之前都没怎么接触过,通过今天的学习了解了这两大功能,也感受到了这两类的强大作用,今天算是收获良多,很期待明天的学习!!!

待续。。。。。将持续更新,感谢大家关注点赞

发布了10 篇原创文章 · 获赞 11 · 访问量 400

猜你喜欢

转载自blog.csdn.net/ywsydwsbn/article/details/104938826