回顾c语言中的文件操作接口
fopen(FILE* fp,char flag);
fclose(FILE* fp);
fwrite(char* msg,int len,size_t count,FILE*fp);
fread(void * msg,size_t size,size_t count,FILE* fp);
其中flag表示打开文件的方式,”w”(只写)”r”(只读)等。
1 #include<stdio.h>
2 #include<string.h>
3 int main(){
4 FILE* fp=fopen("myfile","w");
5 if(!fp){
6 printf("fopen error");
7 return 1;
8 }
9 const char* msg="hello hello\n";
10 int count=5;
11 while(count--){
12 fwrite(msg,strlen(msg),1,fp);
13 }
14 fclose(fp);
15 return 0;
16 }
系统文件I/O
> open read close write
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int open(const char* path,int flags);
int open(const char* path,int flags,mode_t mode);//mode是要设定的权限
int write(int fd,char* buf,int len);//从buf往fd中写
ssize_t read(int fd,buf,size_t size);
close(int fd);
示例:
1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<fcntl.h>
5 #include<unistd.h>
6 #include<string.h>
<