Welcome 微信登录

首页 / 操作系统 / Linux / Linux文件操作函数open close read write等示例

//fileopen.c#include<stdio.h>#include<string.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>int main(){? char temp[]="hello,abc!";int fd;char pathname[255];if((fd=open("fileopen.txt",O_WRONLY|O_CREAT,0640))==-1){printf("creat file wrong!");}int len=strlen(temp)+1;write(fd,temp,len);//若fd为0,或这里直接写0,则会输出到屏幕上而不写入文件中close(fd);} //fileopen2.c
#include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include <stdio.h>int main() {int fa; int s=12;int len;char text[20]="hello,fileopen2!!!";fa=open("fileopen2.txt",O_WRONLY|O_CREAT,0640);        //len = sprintf(text, "%d", s);write(fa,text,sizeof(text)); close(fa); } //test_system_fprintf_fopen.c
#include<stdio.h>#include<stdlib.h>int main(){FILE *stream;char temp[255];char *a="hello,c!";stream=fopen("fprintf.txt","w");fprintf(stream,"%s ",a);fprintf(stream,"sb,c++ ");sprintf(temp,"nl fprintf.txt");fclose(stream);printf("%s ",temp);system(temp);//system函数参数可以为字符串指针或直接字符串system("nl fprintf.txt");exit(0);return 0;}   //file-rw.c
#include<stdio.h>#include<fcntl.h>#include<unistd.h>int main(){int fdr,fdw;char temp[255];char filepath[255];char fileto[255];printf("please input the filepath:");scanf("%s",filepath);if((fdr=open(filepath,O_RDONLY))==-1){printf("file not found,open failed!");return -1;}else{                int r_size=read(fdr,temp,255);close(fdr);sprintf(fileto,"file-rw.txt");if((fdw=open(fileto,O_WRONLY|O_CREAT,0640))==-1){printf("open failed!");return -1;}else{int w_size=write(fdw,temp,r_size);//相当于复制了所输入文件.txt中的255个大小的字符串到新文件file-rw.txt中close(fdw);printf("have successfully copied 255 chars from "%s" to "%s" ",filepath,fileto);}}return 0;}SELinux全面详细介绍Fedora 13安装 Fcitx 4.0.0输入法和设置相关资讯      Linux教程 
  • Linux教程:如何在命令行中查看目  (07/28/2014 12:22:23)
  • Linux 修改root密码  (11/03/2012 07:53:38)
  • su - root 与su root的区别  (06/06/2012 00:39:40)
  • Linux进程间通信:消息队列  (01/28/2013 09:43:00)
  • U盘安装Linux开机无法启动解决方法  (10/07/2012 08:55:52)
  • Windows 7/Linux 同步时间  (05/15/2012 06:17:55)
本文评论 查看全部评论 (0)
表情: 姓名: 字数
版权所有©石家庄振强科技有限公司2024 冀ICP备08103738号-5 网站地图