Linux-文件操作

一:文件的本质:掉电不流失         操作系统上管理数据的一种方式

文件内容操作:创建文件   打开文件    关闭保存

                          读写         调整文件内容指针

文件操作:文件拷贝  文件移动   输出      获取文件信息        

文件映射虚拟内存

二:system调用命令

#include<stdio.h>
#include<stdlib.h>
int main()
{
    system("cp /root/a.txt .");    /*实现文件的拷贝*/
    return 0;
}

三:文件描述符号 

        linux用文件描述符号对应文件,文件描述符号是个整数。

        命令:read        write

        函数:read        write

       注:man open creat

       #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);

#include<stdio.h>
#include <unistd.h>
#include<string.h>
int main()
{
    char buf[256];
    while(1)
    {
     printf("请输入:");
     memset(buff,0,256);  /*清空*/
     read(0,buf,255);  /*读*/
     write(1,buff,strlen(buff));
    }
    return 0;
}

写文件

#include <unistd.h>

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

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
struct student
{
    char name[20];
    int age;
    double score;
};
int main()
{
    struct student s[3]={
        {"小花",18,100},
        {"小草",19,101},
        {"小树",19,102}
    };
    int fd = open("main.txt",O_CREAT|O_WRONLY,0666);
    if(-1==fd)
    {
        printf("创建文件失败:%m\n");
        exit(-1);
    }
    printf("创建文件成功");
    /*循环写入3个*/
    for(int i=0;i<3;i++)
    {
        write(fd,&s[i],sizeof(struct student));
    }
    close(fd);/*关闭才会保存*/  
    return 0;

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值