Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
来源:互联网 发布:一对一私聊软件 编辑:程序博客网 时间:2024/04/30 14:50
Linux的 C系统调用操作 ,下面我们就来练习下 简单的系统调用操作
read系统调用测试
/*************************************************************************> File Name: read.c> Author: > Mail: > Created Time: Tue 10 Feb 2015 01:23:58 AM PST ************************************************************************/#include<stdio.h>#include<unistd.h>#include<errno.h>//利用管道测试程序的标准输入//echo hello world | ./read// ./read < a.txt int main(){ char buf[128] ; int len ; //从输入流读取数据进入到buffer 如果返回-1 那么说明读取错误 if((len=read(0,buf,128))==-1) { //错误写入标准错误输出 write(2,"read error\n",11) ; } //将通过标准输入读取的数据通过标准输出输出到控制台 if(write(1,buf,len)!=len) { //如果输出的长度不等于 len 说明 IO错误 write(2,"read error\n",11) ; } exit(0); return 0 ;}
write系统调用测试
/*************************************************************************> File Name: write.c> Author: > Mail: > Created Time: Tue 10 Feb 2015 12:39:05 AM PST ************************************************************************/#include<stdio.h>//Linux和 Uninx有关与系统调用的封装#include<unistd.h>//errno系统最后一次错误#include<errno.h>//通过自动打开文件句柄 1 也就是标准输出进行字符串输出 int main(){ if(write(1,"hello,world\n",2)!=12) { write(2,"write error!\n",13) ; } printf("ErrorNo:%d\n",errno); exit(0) ;}
open系统调用测试1
/*************************************************************************> File Name: open.c> Author: > Mail: > Created Time: Tue 10 Feb 2015 02:07:54 AM PST ************************************************************************/#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <stdlib.h> int main(){ char*filein="./in.txt" ; char*fileout="./out.txt" ; int fdin,fdout ; char rdChar ; //以只读方式打开输入文件 if((fdin=open(filein,O_RDONLY))==-1) { write(2,"OpenError!\n",11) ; return 0 ; } //打开文件 文件不存在并且创建文件文件存在 会 清空文件 并且复给 文件所有者读写权限 if((fdout=open(fileout,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR))==-1) { //error write(2,"OpenError!\n",11); return 0 ; } //通过系统调用进行文件复制 while(read(fdin,&rdChar,1)==1) { write(fdout,&rdChar,1) ; }exit(0) return 0 ;}
open系统调用测试2
#include<stdio.h>#include<unistd.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/types.h>int main(){ int fdin,fdout,len ; char buf[10] ; fdin=open("./in.txt",O_RDONLY); fdout=open("./out.out",O_WRONLY|O_TRUNC|O_CREAT,S_IRUSR|S_IWUSR) ; //效率比较高的写法 尽量少调用系统调用 while((len=read(fdin,buf,10))>0) { write(fdout,buf,len) ; } return 0 ;
lseek系统调用测试
/*************************************************************************> File Name: lseek.c> Author: > Mail: > Created Time: Tue 10 Feb 2015 05:02:51 AM PST ************************************************************************/#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<fcntl.h>#include<sys/stat.h> #define STD_INPUT 0#define STD_OUTPUT 1#define STD_ERROR 2//函数读取用户输出的第二个和倒数第二个字符 输入字符串大于5int main(){ char input[256] ; int rdLen,wLen ; int fdwrite,fdread; char rdChar; rdLen=read(STD_INPUT,input,256); if(rdLen==-1) { write(STD_ERROR,"error\n",6); exit(0); } if((fdwrite=open("./res.txt",O_CREAT|O_TRUNC|O_WRONLY,S_IRUSR|S_IWUSR))==-1) { write(STD_ERROR,"create file error",17); exit(0) ; } wLen=write(fdwrite,input,rdLen); if(wLen==-1) exit(0); close(fdwrite) ; if((fdread=open("./res.txt",O_RDONLY))==-1) exit(0); lseek(fdread,1,SEEK_SET); read(fdread,&rdChar,1) ; printf("Index2:%c\n",rdChar); lseek(fdread,-3,SEEK_END) ; read(fdread,&rdChar,1); printf("Index-2:%c\n",rdChar); close(fdread) ; return 0 ;}
1 0
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- linux shell 编程笔记 - shell的输入与输出 -3- 标准输入-标准输出-标准错误
- linux 重定向 标准输入 标准输出 标准错误输出
- Linux标准输入,标准输出文件
- Linux:标准输入、输出、错误
- 文件IO---标准输入、标准输出和标准错误
- 标准C文件操作
- 文件操作,标准IO,系统调用
- Linux文件操作,Linux系统函数和C标准库
- linux输出·输入文件和标准输入 标准输出
- linux输出·输入文件和标准输入 标准输出
- Linux系统编程:标准IO操作--索引对应路径下的文件
- Linux标准输入、输出和错误输出
- 【C】标准输入/输出
- 系统的标准输入、输出和错误
- Shell的文件描述符操作,包括标准输入,标准输出,自定义文件描述符
- Linux标准输入、输出
- Linux标准文件IO操作
- 开源项目Philm的MVP架构分析
- C# comboBox 绑定值的方法
- 【HDU】1254 推箱子(BFS DFS HASH记录走过的状态)
- Delphi APP 開發入門(七)通知與雲端推播
- 2015前端设计案例与趋势
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- 通过脚本来配置log4cplus
- java中线程安全的集合对象
- Log4j使用注意点
- iOS 之通知
- Out了,才来开博
- 矩阵三角分解,QR分解,奇异值分解
- 开源UI效果
- TFS 代码 API