body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

读写文件
读写文件的函数原型为:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);    //文件描述词  缓冲区  长度
ssize_t write(int fd, const void *buf, size_t count);
off_t lseek(int fd, off_t offset, int whence);    //用法和fseek一样,只不过第一个形参传递的是fd
对于 read 和 write 函数,出错返回-1,读取完了之后,返回0, 其他情况返回读写的个数。
//rwfile.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
int main(int argc,char **argv)
{
        if( 3!=argc )
        {
                fputs("error args\n",stderr);
                return -1;
        }
        char buf[128]="hello world\n";
        int fdw = open(argv[1],O_CREAT|O_WRONLY,0666);
        if( -1==fdw )
        {
                fputs("error open\n",stderr); //perror(open);
                return -1;
        }
        printf("fdw =%d\n",fdw);
        int ret = write(fdw,buf,strlen(buf)); //sizeof(buf)没字符的地方会写入其他字符
        if( 0>=ret )
        {
                perror("write");
                return -1;
        }
        printf("write ret=%d\n",ret);
        int fdr = open(argv[2],O_CREAT|O_RDONLY,0666);
        if( -1==fdr )
        {
                fputs("error open\n",stderr);  //perror(open);
                return -1;
        }
        printf("fdr =%d\n",fdr);
        bzero(buf,0);
        ret = read(fdr,buf,sizeof(buf));
        printf("read ret=%d\n",ret);
        close(fdw);
        close(fdr);
}

//如果后面不从新的文件中读取数据,那么 fdr 就是3。
//追加内容写入文件会自动换行



O_APPEND  不管光标在哪  都在文件末尾添加

*******为了每次少写头文件,可以vim head.h  里面存放所有要用到的头文件,最后在.c文件中引用,系统会自己去找头文件;vim func.h 存放头文件  vim main.c 中 #include "func.h"  编译.h自动找

宏定义: 标准输入描述符,  标准输出,        标准错误输出

          STDIN_FILENO  0STDOUT_FILENO
1
STDERR_FILENO 2

write(STDOUT_FILENO,"error args\n",11);   等价于

printf("error args\n");

func.h rw_struct.c
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<strings.h>
#include<stdio.h>
//在文件里面写入结构体,能正常写入,但是查看是乱码 fwrite()能够正常写入

#include"head.h"
typedef struct student
{
        int num;
        char name[20];
        float score;
}stu,pstu;
int main(int argc, char **argv)
{
        //stu s[2]={{100,"meihao",89.23},{101,"xiaomei",34.23}};
        stu s[2]={100,"meihao",89.23,101,"xiaomei",34.23};
        printf("%5d %10s %5.2f\n",s[0].num,s[0].name,s[0].score);
        printf("%5d %10s %5.2f\n",s[1].num,s[1].name,s[1].score);
        int fdw = open(argv[1],O_RDWR|O_CREAT,0666);
        write(fdw,&s[1],sizeof(stu));
        lseek(fdw,0,SEEK_SET);
        stu tmp;
        int ret = read(fdw,&tmp,sizeof(stu));
        printf("%5d %10s %5.2f\n",tmp.num,tmp.name,tmp.score);
        close(fdw);
        return 0;
}

标准输入输出文件描述符(区别于文件指针)
与标准的输入输出流对应,在更底层的实现是用标准输入、标准输出、标准错误文件描述符表示的。它们分别用STDIN_FILENOSTDOUT_FILENOSTDERR_FILENO三个宏表示,值分别是0、1、2三个整型数字。
标准输入文件描述符         STDIN_FILENO         0
标准输出文件描述符         STDOUT_FILENO        1
标准错误输出文件描述符     STDERR_FILENO        2
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    char szBuf[32],szBuf2[50];
    printf("Input string:");
    fflush(stdout);   //要刷新标准输出流,才可以立即在屏幕上显示”Input  string”
//fflush用于linux中的时候,只对fflush(stdout)有效。
    int iRet = read(STDIN_FILENO,szBuf,sizeof(szBuf));
    szBuf[iRet]=0;    //read是以无类型指针方式读的数据,不会自动在缓冲区后加0结束标记。
    sprintf(szBuf2,"The string is:%s",szBuf);
    write(STDOUT_FILENO,szBuf2,strlen(szBuf2));
    return 0;
}

LINUX读写文件的更多相关文章

  1. LINUX读写文件区别

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  2. Linux Direct 文件读写(文件DIO)

    有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法: (1)打开文件时,添加O_DIRECT参数: 需要定义_GNU_SOURCE,否则找 ...

  3. 【转】 Linux内核中读写文件数据的方法--不错

    原文网址:http://blog.csdn.net/tommy_wxie/article/details/8193954 Linux内核中读写文件数据的方法  有时候需要在Linuxkernel--大 ...

  4. 【转】在linux内核中读写文件 -- 不错

    原文网址:http://blog.csdn.net/tommy_wxie/article/details/8194276 1. 序曲 在用户态,读写文件可以通过read和write这两个系统调用来完成 ...

  5. Linux一个简单的读写文件

    (1)linux中的文件描述符fd的合法范围是或者一个正正数,不可能是一个负数. (2)open返回的fd程序必须记录好,以后向这个文件的所有操作都要靠这个fd去对应这个文件,最后关闭文件时也需要fd ...

  6. Android 怎样在linux kernel 中读写文件

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  7. linux下c通过虚拟地址映射读写文件的代码

    在代码过程中中,把开发过程中比较好的一些代码片段记录起来,如下的代码内容是关于 linux下c通过虚拟地址映射读写文件的代码,应该对小伙伴有些好处.#include<stdio.h>#in ...

  8. LINUX文件格式化读写(文件指针,缓冲)

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  9. Linux平台下利用系统接口函数按照行读写文件

    要求:支持大文件(1M)一次性读入 源代码如下: #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #i ...

随机推荐

  1. ManyToMany参数(through,db_constraint)

    through : 指定自己写好的第三张表,我们可以给第三张表添加字段了(告诉Django不用建第三张表了,我们都给他配好了) class Book(models.Model): name=model ...

  2. mysql 数据操作 单表查询 having 过滤

    SELECT 字段1,字段2... FROM 库名.表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 1.首先找到表 库.表 ...

  3. SNMP 原理及配置简述 net-snmp-utils net-snmp 第2版基于SNMP 群体名(community name) 第3版引入了安全性更高的访问控制方法 SNMP协议操作只有4种 Apache的php_snmp 模块

    SNMP 原理及配置简述  net-snmp-utils  net-snmp 第2版基于SNMP 群体名(community name) 第3版引入了安全性更高的访问控制方法 SNMP协议操作只有4种 ...

  4. 【开发者笔记】揣摩Spring-ioc初探,ioc是不是单例?

    前言: 控制反转(Inversion of Control,英文缩写为IoC)把创建对象的权利交给框架,是框架的重要特征,并非面向对象编程的专用术语.它包括依赖注入(Dependency Inject ...

  5. HttpClient发送get,post接口请求

    HttpClient发送get post接口请求/*  * post  * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...

  6. django2.0关于path匹配路径页面刷新不出来的问题

    下面是官方文档的内容,如果在urls.py中使用到正则匹配路径(^$)的时候,就需要使用re_path,而不能使用path,不然页面会显示404错误, 如果未用到正则,那么使用path即可. re_p ...

  7. php composer使用过程

    1.安装composer curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/compos ...

  8. 77. Combinations(回溯)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  9. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  10. python socket编程 实现简单p2p聊天程序

    目标是写一个python的p2p聊天的项目,这里先说一下python socket的基础课程 一.Python Socket 基础课程 Socket就是套接字,作为BSD UNIX的进程通信机制,取后 ...