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  0,STDOUT_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_FILENO、STDOUT_FILENO和STDERR_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. ubuntu常用操作命令以及它的通道模式简解

    1.tail tail -f filename :可以动态查看文件的写入,按ctrl+c结束查看. 要显示 notes 文件的最后十行,输入: tail -n -10 notes tail notes ...

  2. ubuntu16.04 安装指定版本Node,升级npm到指定版本

    一.安装配置Node 1.下载(64位系统) wget https://nodejs.org/download/release/v10.1.0/node-v10.1.0-linux-x64.tar.g ...

  3. mono安装

    linux上的DotNET,安装mono 当前,在Linux系统上架设ASP.NET网站.建设WEB应用工程项目已经在国内流行起来,而“Mono+Jexus”架构模式是Linux承载ASP.NET企业 ...

  4. (24)如何使用HttpClient

    介绍 HttpClient是HTTP客户端的接口.HttpClient封装了各种对象,处理cookies,身份认证,连接管理等. 概念 HttpClient的使用一般包含下面6个步骤: 创建 Http ...

  5. 20155203 2016-2017-4 《Java程序设计》第8周学习总结

    20155203 2016-2017-4 <Java程序设计>第8周学习总结 教材学习内容总结 1.channel的继承架构 2.position()类似于堆栈操作中的栈顶指针. 3.Pa ...

  6. python安装mysql-python1.2.5

    首先安装好python 然后安装C++ Microsoft Visual C++ Compiler for Python 2.7 下载后双击安装 登录https://pypi.python.org/p ...

  7. 按月、按日进行数据统计的Mysql语句

    <select id="getCustomerTJByUser" parameterType="map" resultType="map&quo ...

  8. Python笔记 #13# Pandas: Viewing Data

    感觉很详细:数据分析:pandas 基础 import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...

  9. FromBottomToTop第十三周项目博客

    FromBottomToTop第十三周项目博客 本周项目计划 完成游戏核心算法以及界面相关类和怪物类 项目进展 用户可选择游戏模式,共有20张不同的地图. 炮台的建立和升级. 小怪的路径算法. 参考资 ...

  10. linux下 ip指令

    目录 Network ip command Command :ip 简介 内容 Network ip command Command :ip 简介 ip 是個指令喔!並不是那個 TCP/IP 的 IP ...