linux下的文件C编程

文件编程1

文件编程4

Linux文件:

意义:文件本身包含的数据(打开文件可以看到的数据信息)

查看文件属性命令:(ls -l)(元数据:文件的访问权限,文件的大小,创建日期等)
目录也是文件之一

cache:应用程序的缓存文件
opt/run:正在执行着的程序信息,PID文件
opt/tmp:临时文件传递

操作系统:

内核:操作系统对于软硬件和系统资源分配的最重要的核心部分
系统调用:操作系统提供给用户的一组“特殊的”接口,用户可以通过这组接口来获取操作系统内核提供的服务
在这里插入图片描述
系统命令相对于API来说是更高层级
用户编程接口:系统调用并不是直接和程序员进行交互,它仅仅是一个通过软中断机制向内核提交请求,以获取内核服务的接口。在实际使用中程序员调用的通常是用户编程接口(API)
在这里插入图片描述
文件:
文件描述符:本质上是一个正整数
在这里插入图片描述
缓冲区:程序中每个文件都可以使用
磁盘文件->缓冲区->内核(cpu)

文件编程

creat:创建文件函数

int creat (文件描述符,创建模式)

创建模式:
S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
S_IXRWU 可读,可写,可执行

//creat的使用例
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdio.h"

int main()
{
    int fd = creat("./test.txt", S_IRWXU);
    // S_IRUSR 可读 1
    // S_IWUSR 可写 2
    // S_IXUSR 可执行 4
    // S_IRWXU 可读可写可执行 7 (也可以用0777代替)
    if (-1 == fd)
    {
        perror("file create error!");
        return 1;
    }

    return 0;
}

open:打开文件函数

参数列表:(“文件名”,flag:打开的方式,mode如果没有创建那么自动略过)

打开方式:
O_RDONLY:以只读方式打开文件
O_WRONLY:以只写方式打开文件
O_RDWR:以可读可写的方式打开
O_CREAT:如果文件存在就打开,如果不存在就创建。
O_APPEND:写文件的时候追加在文件末尾

函数出错的返回值为-1

//open的使用范例
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdio.h"

int main()
{
    int fd = open("./test.txt", O_RDWR | O_CREAT, 0700);
    // O_RDONLY 只读
    // O_WRONLY 只写
    // O_RDWR  可读可写
    // O_CREAT 存在就打开,不存在就创建
    // O_APPEND 另加在文件结尾

    if (-1 == fd)
    {
        perror("open error!\n");
        return 1;
    }

    return 0;
}

write函数:向文件写入数据

参数列表:(文件描述符,写入的数据统计,写入的数据内存大小)

//write,写入
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "string.h"

int main()
{
    int fd = open("./test.txt", O_RDWR | O_CREAT, 0777);

    if (-1 == fd)
    {
        perror("open!");
        return 1;
    }

    char buff[1024] = "hello";
    write(fd, buff, strlen(buff));
    return 0;
}

read:从文件读数据

参数列表:(文件描述符,读入某个变量(指针))

//包括写入write与读出read的使用
#include "unistd.h"
#include "stdio.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "fcntl.h"
#include "string.h"

int main()
{
    int fd = open("./test.txt", O_RDWR | O_CREAT, 0777);
    if (-1 == fd)
    {
        perror("open");
        return 1;
    }

    char buff[1024] = "hello world what's going on here'";
    write(fd, buff, strlen(buff));
    close(fd);

    int fd1 = open("./test.txt", O_RDWR | O_CREAT, 0777);
    if (-1 == fd1)
    {
        perror("open");
        return 1;
    }

    char buffer[1024] = {0};
    read(fd1, buffer, 20); //限制了读取的长度
    printf("%s", buffer);
    close(fd1);

    return 0;
}

lseek:用来调整光标的位置

参数列表:(文件描述符,光标移动的位置数,光标移动的形式)

SEEK_SET:将光标移动到文件开头再增加相应的offset位置
SEEK_CUR:将光标移动到文件的当前位置再往后加offset位置
SEEK_END:将光标移动到文件的末尾再增加offset位置

lseek函数:返回值是从文件开头到光标位置的字节个数

//lseek的使用
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "string.h"

int main()
{
    int fd = open("./test.txt", O_RDWR | O_CREAT, 0777);
    if (-1 == fd)
    {
        perror("open");
        return 1;
    }

    char buff[1024] = "hello world what's going on here'";
    write(fd, buff, strlen(buff));
    close(fd);

    int fd1 = open("./test.txt", O_RDWR | O_CREAT, 0777);
    if (-1 == fd1)
    {
        perror("open");
        return 1;
    }

    char buffer[1024] = {0};
    lseek(fd1, 2, SEEK_SET); //在这里定义光标开始的位置
    read(fd1, buffer, 20);   //限制了读取的长度
    printf("%s", buffer);
    close(fd1);

    return 0;
}

ftruncate:将指定的文件大小修改成length指定的大小(用来给文件扩容(如果当前文件指定的大小小于当前文件,结果是超出的数据被删除)

参数列表:ftruncate(int fd, length)

未举例

stdio.h:C的标准输入输出库:I/O(input output)

在这里插入图片描述

stdout:输出流
行缓冲:stdout在终端上进行输出的时候,输出的规则为每当出现换行符的时候进行一次刷新缓存,然后再进行操作(printf输出的时候,是看到换行符才进行输出)

fopen: 打开文件的函数

r:只读
w:只写(如果文件不存在,创建一个新的,如果文件存在,清空原先文件的文件内容)
a:追加(如果文件不存在,创建一个新的,如果存在文件,则在文件末尾进行追加)
r+:可读可写,文件不存在,则打开失败
w+:可读可写(如果文件不存在,创建一个新的,如果文件存在,清空原先文件的文件内容)
a+ :可读可写(如果文件不存在,创建一个新的,如果存在文件,则在文件末尾进行追加)

//fopen的使用
#include "stdio.h"

int main()
{    
    FILE *fp;
    fp = fopen("../read/test.txt", "r");
    //r :只读
    //w :只写
    //a :追加
    //r+ :可读可写,文件不存在,则打开失败
    //w+ :可读可写,文件不存在,则创建,存在,则清空之前内容
    //a+ :可读可写,文件不存在,则创建,存在,则在之前的文件末尾追加
    if (fp == NULL)
    {
        perror("fopen!");
        return 1;
    }

    else
    {
        char buff = fgetc(fp);
        while(!feof(fp))
        //对feof()来说,它的工作原理是,站在光标所在位置,向后看看还有没有字符。如果有,返回0;如果没有,返回非0。
        //文件描述符指向的文件读完前,本行相当于while(1)
        {
            putchar(buff);
            buff = fgetc(fp);
        }
        fclose(fp);
    }
    return 0;
}

fopen与之对应的,结尾要加上fclose(fp):

fread:读文件的函数

参数列表:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
ptr:字符串指针
size:读取每个字符的大小
nmemb:读多少
stream:文件描述符指针

//fwirte的使用举例
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
    FILE *fp;
    fp = fopen("./a.txt", "w+");
    if (NULL == fp)
    {
        perror("open");
        exit(1);
    }
    char *str = "hello world";
    int number = fwrite(str, sizeof(char), strlen(str), fp);

    if (0 == number)
    {
        perror("fwrite:");
        fclose(fp);
        exit(1);
    }

    fclose(fp);
    return 0;
}

fseek

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
    FILE *fp;
    fp =fopen("../read/test.txt", "r");
    if(NULL == fp)
    {
        perror("fopen!");
        return 1;
    }
    
    fseek(fp, 2, SEEK_SET);
    char buffer[1024] = {0};
    int num2 = fread(buffer, sizeof(char), 1024, fp);
    if (0 == num2)
    {
        perror("fread!");
        fclose(fp);
        return 1;
    }

    printf("%s",buffer);
    fclose(fp);
    return 0;
}

读字符的函数:

getc:从文件里读取一个字符getc(fp)返回值;当读到文件末尾无字符的时候,返回EOF(NULL)
fgetc:函数调用
getchar():将字符从unsigned char 转换成int进行返回,带走缓冲区中多余的换行符

fputc:将某一字符写入文件流

参数列表:int fputc (int c,FILE *stream)

//fputc函数使用
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
    FILE *fp;
    fp = fopen("./test.txt", "w+");
    char *str = "hello world world world world";

    int i;
    for (i = 0; i < strlen(str); i++)
    {
        fputc(*(str + i), fp); //向文件中逐个写入
    }

    fseek(fp, 0, SEEK_SET);
    char buffer[1024] = {0};
    int num2 = fread(buffer, sizeof(char), 1024, fp);
    if (0 == num2)
    {
        perror("fread!");
        fclose(fp);
        return 1;
    }

    printf("%s", buffer);
    fclose(fp);
    return 0;
}

fprintf:向文件中写入

参数列表:int fprintf(FILE *stream, const char *format, …);

#include"stdio.h"
#include"stdlib.h"
#include"string.h"

int main()
{
    FILE *fp;
    int i=10;
    int j=20;
    
    double l =3.1415926;

    fp =fopen("./a.txt","w+");

    fprintf(fp, "%d,%d,%.7f",i,j,l);
    printf("\n");
    return 0;
}

sprintf():往字符串里写字符串

fscanf () :从文件流里写读字符串

参数列表:int fscanf(FILE *stream, const char *format, …);

sscanf ():从字符串里读取变量

参数列表:int sscanf(const char *str, const char *format, …);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值