首页 > 编程语言 >文件编程(一)

文件编程(一)

时间:2023-04-26 23:25:36浏览次数:31  
标签:文件 file1 int 编程 write fd include open

1.文件编程概述
api  open
    read/write
    lseek
    close


2.文件打开和创建
#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)
int creat(const char *pathname,mode_t mode)

pathname:要打开的文件名
flags: O_RDONLY只读打开 O_WRONLY只写打开 O_RDWR可读可写打开
下列常数是可以选择的:
O_CREAT若文件不存在则创建它,在使用此选项时,需要同时说明第三个参数mode
O_EXCL如果同时指定了O_CREAT,而文件已经存在,则出错
O_APPEND每次写时都加到文件的末端,不写会覆盖
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0

Mode:一定是在flags中使用O_CREAT标志,mode记录待创建的文件的访问权限

open的返回值就是文件描述符

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

int main(){
        int fd;

        fd = open("./file1",O_RDWR);
  
        if(fd == -1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }       
        printf("fd = %d\n",fd);

        return 0;
}

3.创建文件creat函数

int creat(const char *filename,mode_t mode)
filename:要创建的文件名
mode:创建模式
常见创建模式:
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读可写可执行

4.文件写入操作编程
#include<unistd.h>

ssize_t write(int fd,const void *buf,size_t count)

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


int main(){
        int fd;
        char *buf = "linqinlaoshi hen shuai!";

        fd = open("./file1",O_RDWR);

        if(fd == -1){
                printf("open file1 failed");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success");
                }
        }
        printf("open sucess fd = %d\n",fd);

        write(fd,buf,strlen(buf));

        close(fd);

        return 0;
}

5.文件读取操作
#include<unistd.h>

ssize_t read(int fd,void *buf,size_t count)

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


int main(){
    int fd;
    char *buf = "linqinlaoshi hen shuai!";

    fd = open("./file1",O_RDWR);

    if(fd == -1){
        printf("open file1 failed\n");
        fd = open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
            printf("creat file1 success");
        }
    }    
    printf("open sucess fd = %d\n",fd);
    

    int n_write = write(fd,buf,strlen(buf));
    if(n_write !== -1){
        printf("write %d byte to file\n",n_write);
    }
    
    close(fd);
    fd = open("./file1",O_RDWR);    
    
    char *readBuf;
    readBuf = (char *)malloc(sizeof(char)*n_write +1);
    int n_read = read(fd,readBuf,n_write);

    printf("read %d ,context:%s\n ",n_read,readBuf);
    close(fd);

    return 0;
}

6.文件光标移动操作
文件“光标”位置
将文件读写指针相对whence移动offset个字节
#include<sys/types.h>
#include<unistd.h>

off_t lseek(int fd,off_t offset,int whence)
SEEK_SET 指向文件的头
SEEK_GND 指向文件的尾
SEEK_END 当前位置

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


int main(){
        int fd;
        char *buf = "linqinlaoshi hen shuai!";

        fd = open("./file1",O_RDWR);

        if(fd == -1){
                perror("open file1 failed\n");
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success");
                }
        }
        printf("open sucess fd = %d\n",fd);


        int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }

        lseek(fd,0,SEEK_SET);

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write +1);
        int n_read = read(fd,readBuf,n_write);

        printf("read %d ,context:%s\n ",n_read,readBuf);
        close(fd);

        return 0;
}

 

标签:文件,file1,int,编程,write,fd,include,open
From: https://www.cnblogs.com/Lynchteacher/p/17357697.html

相关文章

  • 编程一小时2023.4.26
    1.#include<iostream>usingnamespacestd;intmain(){intn,a;cin>>n;for(inti=1;i<=n;i++){cin>>a;if(a%4==0)printf("%d%d\n",a/4,a/2);elseif(a%2==0)printf("%......
  • SpringMcv 文件上传下载
    文件上传SpringMVC为文件上传提供了直接的支持,这种支持是通过即插即用的MultipartResolver实现的。Spring用JakartaCommonsFileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResovlerSpringMVC上下文中默认没有装配MultipartResovler,因此默认情况......
  • 数据存储与访问——文件存储读写
    本节给大家介绍的是Android数据存储与访问方式中的一个——文件存储与读写,当然除了这种方式外,我们可以存到SharedPreference,数据库,或者Application中。1.Android文件的操作模式学过Java的同学都知道,我们新建文件,然后就可以写入数据了,但是Android却不一样,因为Android是基于Linux的,我......
  • 如何在博客园快速上传Markdown文件
    如何在博客园快速上传Markdown文件1、首先拥有书写MarkDown文件的工具:例如:Typora(博主推荐使用)MarkdownPadBookPad小书匠VisualStudioCode等等下载Typora的地址(自取):MarkDown软件https://www.aliyundrive.com/s/vnBazjXLdkr提取码:tx58点击链接保存,或者复制本段内......
  • 在linux中如何读取使用tcpdump命令抓取保存的tcpdump capture file类型的数据文件
    笔者在之前的文章中,说明了如何在linux使用tcpdump命令进行抓包,以及将抓包结果保存到文件具体操作,可以参考:https://www.cnblogs.com/5201351/p/17357444.html如果是使用tcpdump命令,-wxxxxxx.dump这种方式保存的文件,我们可以通过file命令发现其文件类型[root@localhostqq-52......
  • 一篇文章告诉你金融行业如何高效管理文件
    由于金融行业的行业属性,信息安全万分重要。因此在文件管理工具时,要注意数据安全问题,那么金融行业如何高效管理文件呢?首先金融行业在文件管理时可能面临以下问题:1,资料繁杂,整理困难,使用不便;2,传统文件协作方式存在安全隐患;3,当员工出差时,无法及时调取需要的文件。ZohoWorkdrive企业网......
  • spring jdbc 编程式事务
    所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理。新建maven工程,pom文件如下:<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http:/......
  • FLV文件分析
    很久没看,做下关于FLV文件格式知识点回顾!一、简单介绍    FLV(FlashVideo)是Adobe公司推出的一种媒体封装格式。一个FLV文件,每个Tag类型都属于一个流。也就是说一个FLV文件最多只有一路音频流、一个路视频流,不能定义单个类型的多个独立流在一个文件中。    FLV数......
  • java 并发编程-基础篇
    java创建线程的三种方法直接使用Thread//创建线程对象Threadt=newThread(){publicvoidrun(){//要执行的任务}};//启动线程t.start();Runable配合Thread把线程和任务分开。Runnablerunnable=newRunnable(){publicvoidrun(......
  • 实验3 控制语句与组合数据类型应用编程
    1。实验任务1task1.py1importrandom23print('用列表储存随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合储存随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print(&#......