linux c 文件 read(读) 和 write (写) 代码分析

简介: read code: [root@luozhonghua 03]# cat ex03-read-01.c /*文件ex03-open-03.c, O_CREAT和O_EXCL的使用*/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include &lt

read code:


[root@luozhonghua 03]# cat ex03-read-01.c

/*文件ex03-open-03.c,
O_CREAT和O_EXCL的使用*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>


int main(void)
{
  int fd = -1,i;
  ssize_t size = -1;
  /*存放数据的缓冲区*/
  char buf[10];
  char filename[] = "test.txt";
  /*打开文件,如果文件不存在,则报错*/
        fd = open(filename,O_RDONLY);
        if(-1 == fd){
                /*文件已经存在*/
          printf("Open file %s failure,fd:%d\n",filename,fd);
        }       else    {
                /*文件不存在,创建并打开*/
          printf("Open file %s success,fd:%d\n",filename,fd);
        }


        /*循环读取数据,直到文件末尾或者出错*/
        while(size){
                /*每次读取10个字节数据*/
                size = read(fd, buf,10);
                if( -1 == size) {
                        /*读取数据出错*/
                        close(fd);/*关闭文件*/
                        printf("read file error occurs\n");
                        /*返回*/
                        return -1;
                }else{
                        /*读取数据成功*/
                        if(size >0 ){
                                /*获得size个字节数据*/
                                printf("read %d bytes:",size);
                                /*打印引号*/
                                printf("\"");
                                /*将读取的数据打印出来*/
                                for(i = 0;i<size;i++){
                                        printf("%c",*(buf+i));
                                }
                                /*打印引号并换行*/
                                printf("\"\n");
                        }else{
                                printf("reach the end of file\n");
                        }
                }
        }


        return 0;

}


[root@luozhonghua 03]# ./ex03-read-01
Open file test.txt success,fd:3
read 10 bytes:"aaaaaaaaaa"
read 10 bytes:"aaaaaaaaaa"
read 10 bytes:"aaaaaaaaaa"
read 5 bytes:"aaaa
"
reach the end of file


-----write


[root@luozhonghua 03]# cat ex03-write-01.c
/*文件ex03-write-01.c,
O_CREAT和O_EXCL的使用*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>


int main(void)
{
  int fd = -1,i;
  ssize_t size = -1;
  int input = 0;
  /*存放数据的缓冲区*/
  char buf[]="quick brown fox jumps over the lazy dog";
  char filename[] = "test.txt";
  /*打开文件,如果文件不存在,则报错*/
        fd = open(filename,O_RDWR|O_TRUNC);
        if(-1 == fd){
                /*文件已经存在*/
          printf("Open file %s failure,fd:%d\n",filename,fd);
        }       else    {
                /*文件不存在,创建并打开*/
          printf("Open file %s success,fd:%d\n",filename,fd);
        }


        /*将数据写入到文件test.txt中*/
        size = write(fd, buf,strlen(buf));
        printf("write %d bytes to file %s\n",size,filename);
        /*关闭文件*/
        close(fd);


        return 0;
}
[root@luozhonghua 03]# cat text.txt
cat: text.txt: No such file or directory
[root@luozhonghua 03]# cat test.txt
quick brown fox jumps over the lazy dog








目录
相关文章
|
2天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
2天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
3 0
|
7天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
16 0
|
8天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
82 0
|
10天前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
4月前
|
Shell Linux 数据安全/隐私保护
Linux命令(78)之read
Linux命令(78)之read
48 3
|
Linux Shell 开发工具
|
4天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
22小时前
|
监控 Linux Windows
50个必知的Linux命令技巧,你都掌握了吗?(下)
50个必知的Linux命令技巧,你都掌握了吗?(下)
|
1天前
|
Linux Shell Windows
Linux 常用基本命令
Linux 常用基本命令