文章目录
- 一、文件写入
- 二、文件读取
- 三、文件光标移动
- 使用 `lseek()` 计算文件大小
一、文件写入
write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd.write()
write() 函数,将从buf缓冲区开始,写count个字节,写入到文件描述符fd引用的文件。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main()
{
int fd;
char *buf = "changxianrui hen shuai !!!";
fd = open("./file", O_RDWR);
if(fd == -1){
printf("open file failed\n");
fd = open("./file", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("create file success");
}
}
printf("open success: fd=%d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
strlen()
函数用来统计字符串的长度,遇到\0
结束统计,即函数返回值不包括\0
。例如:strlen("abc")
的返回值为3,但是sizeof("abc")
的返回值是4,这里要留意。
二、文件读取
如果读取不到内容可能是光标的问题 重新打开或者移动光标
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
read() 从文件描述符fd,读取count个字节的数据,存到buf缓冲区。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "changxianrui hen shuai !!!";
fd = open("./file", O_RDWR);
if(fd == -1){
printf("open file failed\n");
fd = open("./file", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("create file success\n");
}
}
printf("open success: fd=%d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd, buf, strlen(buf));
if(n_write != -1){
printf("write %d bytes to file\n", n_write);
}
close(fd);
fd = open("./file", O_RDWR);
char *readBuf;
readBuf = malloc(sizeof(char) * n_write + 1); // 没有这步段错误
// ssize_t read(int fd, void *buf, size_t count);
int n_read = read(fd, readBuf, n_write);
printf("read %d bytes, context:%s\n", n_read, readBuf);
close(fd);
return 0;
}
三、文件光标移动
lseek()
函数的作用:让文件指针相对whence
参数移动offest
字节,offest
为正数则向后移动,反之负数向前移。
文件指针是你在这个文件中所处的位置,进行读写操作的时候,都是从文件指针所在的位置开始。
lseek()
函数的返回值是距离文件头的字节数。
可以利用移动光标读文件。因为先写文件,后读文件,移动光标避免读不到。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "changxianrui hen shuai !!!";
fd = open("./file", O_RDWR);
if(fd == -1){
printf("open file failed\n");
fd = open("./file", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("create file success\n");
}
}
printf("open success: fd=%d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd, buf, strlen(buf));
if(n_write != -1){
printf("write %d bytes to file\n", n_write);
}
char *readBuf;
readBuf = malloc(sizeof(char) * n_write + 1); // 没有这步段错误
// ssize_t read(int fd, void *buf, size_t count);
// off_t lseek(int fd, off_t offset, int whence);
//lseek(fd, 0, SEEK_SET);
lseek(fd, -26, SEEK_END);//正数向后移动,负数向后移动
int n_read = read(fd, readBuf, n_write);
printf("read %d bytes, context:%s\n", n_read, readBuf);
close(fd);
return 0;
}
使用 lseek()
计算文件大小
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
fd = open("./file", O_RDWR);
int size = lseek(fd, 0, SEEK_END);//正数向后移动
printf("read %d bytes\n", size);
close(fd);
return 0;
}