ARM学习笔记2-文件IO操作
int fd = creat(路径,权限);
ssize_t fd2 = write(句柄,数据,数据长度);
ssize_t fd2 = write(句柄,数据,数据长度);
int main(void){
int fd = 100; #句柄
char *path = "./CreatedFile";#创建文件路径
fd = creat(path,0777);#文件路径和文件创建权限
if(fd != -1){
printf("Creat success!%s is %d\n",path,fd);#成功之心这里
}
else{
printf("CreatError!fd = %d\n",fd); #失败打印这里
}
return 0;
}
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
int main(void){
int fd = 100;
ssize_t fd2;
char *path = "./hello.c";
char *things = "#include <stdio.h>";
fd = open(path,O_RDWR|O_CREAT);
if(fd != -1){
printf("Open Succ!\n");
}
else{
perror("Open Fail!\n");
}
fd2 = write(fd,things,strlen(things));
if(fd2 != -1){
printf("\nWrite Succ!\n");
}
else{
printf("\nWrite Failed\n");
}
close(fd);
return 0;
}
ssize_t rd = read(句柄,存储数据的头指针,要存储数据的长度);
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MAX 1000
int main(void){
int a;
ssize_t rd = 999;
int fd = 999;
char *path = "./hello.c";
char read_buf[MAX];
fd = open(path,O_RDWR);
if(fd != -1){
printf("Open success!\n");
}
else{
perror("Open failed!\n");
return -1;
}
rd = read(fd,read_buf,MAX);
if(rd != -1){
for(a = 0; read_buf[a] != 'X'; a++){
printf("%c",read_buf[a]);
}
printf("\n");
}
else{
printf("read failed!\n");
return -1;
}
close(fd);
return 0;
}