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. 最长括号化长度 java

    1:求最长括号, ()(()()( 例如,它的最长符合括号化的长度为4 package com.li.huawei; import java.util.Arrays; import java.util ...

  2. PAT 1090 Highest Price in Supply Chain[较简单]

    1090 Highest Price in Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors ...

  3. linux磁盘空间使用问题

    linux磁盘空间用满的处理方法 linux下空间满可能有两种情况 可以通过命令 df -h  查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) df -i  查看索引节点的占用(Inod ...

  4. 【运维技术】slc pm 启动不了,异常排除问题记录

    问题描述 slc pm 启动的时候报错如下: [root@iZuf61qpjpeuqc5mjo4kn8Z lixiang-scf-web]# slc pm strong-remoting deprec ...

  5. monit拉起服务

    check process hive_metastore matching "HiveMetaStore" start program = "/usr/bin/nohup ...

  6. sqlite的缺点和限制

    随着查询变大变复杂,查询时间使得网络调用或者事务处理开销相形见绌, 这时一些大型的设计复杂的数据库开始发挥作用了. 虽然SQLite也能处理复杂的查询,但是它没有精密的优化器或者查询计划器. SQLi ...

  7. 如何让.gitignore文件生效

    改动过.gitignore文件之后,在repo的根目录下运行 # 先将当前仓库的文件的暂存区中剔除 git rm -r --cached . # 再添加所有的文件到暂存区,这时.gitignore文件 ...

  8. Ubuntu 安装zookeeper

    下载zookeeper   Zookeeper下载 下载以后将文件迁移到/home/Hadoop/文件夹下面 hongdada@ubuntu:~/Downloads$ sudo mv zookeepe ...

  9. 中通快递单api查询

    request POST https://hdgateway.zto.com/WayBill_GetDetail HTTP/1.1Host: hdgateway.zto.comConnection: ...

  10. APP AutoTestCaseID

    public class AutoTestCaseID { ElementExist el = new ElementExist(); static AutoTestExcelFile ft = ne ...