嵌入式应用开发学习笔记——文件IO
最近在学习嵌入式应用编程,这里用来记录一些学习笔记,希望对自己有所帮助!
文件模式
文件模式是文件属性之一,占16bit
访问权限占9bit,即rwx-rwx-rwx,修饰位占3bit,文件类型占4bit
命令:umask设置新建文件的权限,umask是新创建文件、目录应关闭权限的位掩码
命令:chmod修改文件权限
系统调用
创建
int creat(const char *filename, mode_t mode)
成功返回fd, 失败返回-1
filename:要创建的文件名(包含路径,缺省为当前路径)
mode:创建模式(S_IRUSR-可读,S_IWUSR-可写,S_IXUSR-可执行,S_IRWXU-可读、写、执行等模式),可以通过man creat查看帮助
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4
5 int main(void)
6 {
7 int fd;
8 fd = creat("1.txt", 0666);
9 if(fd < 0)
10 {
11 perror("create fail");
12 exit(1);
13 }
14 printf("file descriptor is %d\n", fd);
15 close(fd);
16 return 0;
17 }
打开
int open(const char *pathname, int flags)
int open(const char *pathname, int flags, mode_t mode)
成功返回fd, 失败返回-1
pathname:要打开的文件名
flags:打开标志,常用的打开标志(O_RDONLY只读方式打开,O_WRONLY只写方式打开,O_RDWR读写方式打开,O_APPEND追加方式打开,O_CREAT创建一个文件,O_NOBLOCK非阻塞方式打开),如果使用了O_CREAT标志,则使用的函数是:
int open(const char *pathname, int flags, mode_t mode);这时需要定mode来表示文件的访问权限
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7
8 int main(void)
9 {
10 int fd;
11 fd = open("2.txt", O_RDONLY|O_CREAT, 0666);
12 if(fd < 0)
13 {
14 perror("open and create 2.txt fail!");
15 exit(1);
16 }
17 printf("fd = %d\n", fd);
18 close(fd);
19 return 0;
20 }
关闭
int close(int fd)
成功返回0,失败返回-1
读写
int read(int fd, void *buf, size_t length)
功能:从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数。
返回读取到的字符数,错误返回-1,读到文件末尾返回0
int write(int fd, void *buf, size_t length)
功能:把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数。
返回写入的字符数,错误返回-1,读到文件末尾返回0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8
9 int main(void)
10 {
11 int readnum;
12 int fd;
13 char buf[1024];
14 fd = open("test.txt", O_RDONLY);
15 if(fd < 0)
16 {
17 perror("open fail");
18 exit(1);
19 }
20 memset(buf, 0, sizeof(buf));
21 readnum = read(fd, buf, 1024);
22 if(readnum < 0)
23 {
24 perror("read wrong!");
25 exit(1);
26 }
27 else if(readnum > 0)
28 {
29 printf("readnum from file is %s\n", buf);
30 }
31 close(fd);
32 return 0;
33 }
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <string.h>
7
8 int main(void)
9 {
10 int writenum;
11 int fd;
12 char buf[1024];
13 fd = open("test.txt", O_WRONLY|O_CREAT, 0666);
14 if(fd < 0)
15 {
16 perror("open fail");
17 exit(1);
18 }
19 memset(buf, 0, sizeof(buf));
20 strcpy(buf, "how do you do");
21 writenum = write(fd, buf, strlen(buf));
22 if(writenum < 0)
23 {
24 perror("write wrong");
25 exit(1);
26 }
27
28 close(fd);
29 return 0;
30 }
定位
int lseek(int fd, offset_t offset, int whence)
功能:将文件读写指针相对whence移动offset个字节,操作成功时,返回文件指针相对于文件头的位置。
whence可使用下述值:
SEEK_SET-相对文件开头,SEEK_CUR-相对文件读写指针的当前位置,SEEK_END-相对文件末尾
offset可取负值,表示向前移动,例如:lseek(fd, -5, SEEK_CUR),可将文件指针对当前位置向前移动5个字节。
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <string.h>
7
8 int main(void)
9 {
10 char buf[1024];
11 int readnum = 0;
12 int writenum = 0;
13 int fd = open("test.txt", O_RDWR|O_CREAT, 0666);
14 if(fd < 0)
15 {
16 perror("open");
17 exit(1);
18 }
19 memset(buf, 0, sizeof(buf));
20 strcpy(buf, "this is my file to test lseek");
21 writenum = write(fd, buf, strlen(buf));
22 if(writenum < 0)
23 {
24 perror("write");
25 exit(1);
26 }
27 lseek(fd, 5, SEEK_SET);
28
29 memset(buf, 0, 1024);
30 readnum = read(fd, buf, sizeof(buf));
31 if(readnum < 0)
32 {
33 perror("read");
34 exit(1);
35 }
36 else if(readnum == 0)
37 {
38 printf("end of file \n");
39 }
40 else
41 {
42 printf("read from file : %s\n", buf);
43 }
44 close(fd);
45 return 0;
46 }
如何利用lseek来计算文件长度?
由于lseek函数的返回值为文件指针相对于文件头的位置,因此下面调用的返回值就是文件的长度:
lseek(fd, 0, SEEK_END)
访问判断
int access(const char *pathname, int mode)
功能:判断文件是否可以进行某种操作(读、写等)
pathname:文件名称
mode:要判断的访问权限,可以取一下值或组合:R_OK-文件可读,W_OK-文件可写,X_OK-文件可执行,F_OK-文件存在
返回值:成功时,函数返回0,否则如果一个条件不符时,返回-1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5
6 int main(void)
7 {
8 int res;
9 res = access("test.txt", F_OK);
10 if(res == 0)
11 {
12 printf("test.txt file exist\n");
13 }
14 else
15 {
16 printf("test.txt file not exist\n");
17 }
18 return 0;
19 }