LINUX读写文件
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读写文件的更多相关文章
- LINUX读写文件区别
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Linux Direct 文件读写(文件DIO)
有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法: (1)打开文件时,添加O_DIRECT参数: 需要定义_GNU_SOURCE,否则找 ...
- 【转】 Linux内核中读写文件数据的方法--不错
原文网址:http://blog.csdn.net/tommy_wxie/article/details/8193954 Linux内核中读写文件数据的方法 有时候需要在Linuxkernel--大 ...
- 【转】在linux内核中读写文件 -- 不错
原文网址:http://blog.csdn.net/tommy_wxie/article/details/8194276 1. 序曲 在用户态,读写文件可以通过read和write这两个系统调用来完成 ...
- Linux一个简单的读写文件
(1)linux中的文件描述符fd的合法范围是或者一个正正数,不可能是一个负数. (2)open返回的fd程序必须记录好,以后向这个文件的所有操作都要靠这个fd去对应这个文件,最后关闭文件时也需要fd ...
- Android 怎样在linux kernel 中读写文件
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- linux下c通过虚拟地址映射读写文件的代码
在代码过程中中,把开发过程中比较好的一些代码片段记录起来,如下的代码内容是关于 linux下c通过虚拟地址映射读写文件的代码,应该对小伙伴有些好处.#include<stdio.h>#in ...
- LINUX文件格式化读写(文件指针,缓冲)
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Linux平台下利用系统接口函数按照行读写文件
要求:支持大文件(1M)一次性读入 源代码如下: #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #i ...
随机推荐
- requests获取所有状态码
requests获取所有状态码 requests默认是不会获取301/302的状态码的.可以设置allow_redirects=False,这样就可以获取所有的状态码了 import requests ...
- 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat
解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat ...
- thinkphp5使用PHPMailler发送邮件
http://www.dawnfly.cn/article-1-350.html 想要了解thinkphp3.2版本发送邮件的,请点击此链接:http://www.dawnfly.cn/article ...
- 【译】3 ways to define a JavaScript class
本文真没啥难点,我就是为了检验我英语水平退化了没哈哈虽然我英语本来就渣翻译起来也像大白话.将原文看了一遍也码完翻译了一遍差不多一个小时,其中批注部分是自己的理解如有疏漏或误解还请指出感激不尽呐,比如J ...
- AngularJS SQL
服务端代码 以下列出了列出了几种服务端代码类型: 使用 PHP 和 MySQL.返回 JSON. 使用 PHP 和 MS Access.返回 JSON. 使用 ASP.NET, VB, 及 MS Ac ...
- linux进程间通讯的几种方式的特点和优缺点
# 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系.# 有名管道 (named pipe) : 有名管道也是 ...
- 常微分方程初值问题:多步预测-修正方法 [MATLAB]
#先上代码后补笔记# #可以直接复制粘贴调用的MATLAB函数代码!# 1. 亚当斯(Adams)预测-修正算法 由亚当斯-巴什福特(Adams-Bashforth)显式预测公式和亚当斯-莫顿(Ada ...
- bzoj1009 / P3193 [HNOI2008]GT考试
P3193 [HNOI2008]GT考试 设$f[i][j]$表示主串匹配到第$i$个位置,不吉利数字匹配到第$j$个位置 $g[i][j]$表示加上某数字使子串原来最多能匹配到第$i$个数字,现在只 ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- DataSet 和 DataTable 以及 DataRow
向DataSet中添加DataTable 会提示datatable已属于另一个dataset 本来的想法是每次都new一个DataTable,但是还是会报错 百度了一下,发现可以调用DataTable ...