Linux 文件操作-文件IO函数2- write向文件写入数据、read从文件读取数据、lseek重定位文件描述符的偏移量的验证
目录
1.write 向文件描述符中写入数据
1.1 向标准输出STDOUT_FILENO (终端屏幕)写入数据
1.2 向文件写入数据
2.read 从文件描述符中读取数据
2.1从标准输入STDIN_FILENO(键盘)读取数据
2.2从文件读取数据
3.lseek重定位偏移量
1.write 向文件描述符中写入数据
功能:
向文件描述符中写入buf指向的count个字节数据
函数原型:
#include <unistd.h>
/* 参数:
fd:文件描述符
buf:存放数据空间首地址
count:写入数据的字节数
返回值:
成功返回实际写入字节数
失败返回-1
没有写入任何东西返回0 */
ssize_t write(int fd, const void *buf, size_t count);
1.1 向标准输出STDOUT_FILENO (终端屏幕)写入数据
程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
//向标准输出STDOUT_FILENO 写入数据
int test2()
{
printf(" test2: \n");
ssize_t num;
const char *buf1 = "ABCDEFG\n";
//STDOUT_FILENO = 1;//标准输出
num = write(STDOUT_FILENO, buf1, strlen(buf1));
//num = write(1, buf1, strlen(buf1));
printf(" write size = %d\n", num);//写入字节大小
printf(" 向标准输出写入数据 \n");
if( num == -1)
{
printf(" fail to write \n");
return -1;
}
else if( num == 0)
{
printf(" write size is 0 \n");
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
test2();
return 0;
}
运行结果:ABCDEFG\n 为write函数写入的数据
1.2 向文件写入数据
程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int test1() // ssize_t
{
int fd = 0;
//只写打开,不存在创建,存在清0
fd = open("./file2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (-1 == fd)
{
perror("fail to open file ");
return -1;
}
ssize_t num;
char buf[] = {"hello world ABC"};
num = write(fd, buf, strlen(buf));
if( num == -1)
{
printf(" fail to write \n");
}
else if( num == 0)
{
printf(" write size is 0 \n");
}
printf("strlen(buf) = %d\n", strlen(buf));
printf("buf = %s\n", buf);
printf("write size = %d\n", num);//向文件写入字节大小
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
test1();
return 0;
}
运行结果:
2.read 从文件描述符中读取数据
功能:
从文件描述符fd中读count个字节存放到buf指向的空间中
函数原型:
#include <unistd.h>
/* 参数
fd:源文件描述符(如文件、标准输入 STDIN_FILENO、套接字等)。
buf:存储读取数据的缓冲区地址。
count:期望读取的最大字节数。
返回值:
成功:返回实际读取的字节数(可能小于 count,0 表示到达文件末尾)。
失败:返回 -1,错误码保存在 errno 中。 */
ssize_t read(int fd, void *buf, size_t count);
2.1从标准输入STDIN_FILENO(键盘)读取数据
程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
//从标准输入 键盘读取输入的数据
int test2()
{
printf(" test2: \n");
ssize_t num;
char buf1[64];
printf(" 从标准输入 键盘读取数据,请输入数据: \n");
//STDIN_FILENO = 0;//标准输入
// 需预留一个字节给\0字符串终止符
num = read(STDIN_FILENO, buf1, sizeof(buf1) - 1);
printf(" read size = %d\n", num);//读取字节大小
if( num == -1)
{
printf(" fail to read \n");
return -1;
}
else if( num == 0)
{
printf(" read end \n");
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
test2();
return 0;
}
运行结果:
2.2从文件读取数据
程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
//从文件读取内容
int test1()
{
int fd = 0;
//只读打开
fd = open("./file2.txt", O_RDONLY);
if (-1 == fd)
{
perror("fail to open file ");
return -1;
}
ssize_t num;
char buf[64] ;
num = read(fd, buf, sizeof(buf));
if( num == -1)
{
printf(" fail to read \n");
return -1;
}
else if( num == 0)
{
printf(" read end \n");
return 0;
}
// printf("strlen(buf) = %d\n", strlen(buf));
printf("read size = %d\n", num);//读取字节大小
printf("read buf = %s\n", buf);
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
test1();
return 0;
}
运行结果:
3.lseek重定位偏移量
功能:
重定位一个文件描述符的偏移量
函数原型:
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fd, off_t offset, int whence);
/* 参数:
fd:文件描述符
offset:偏移量
> 0 向后偏移
< 0 向前偏移
whence:基准位置,可选值
SEEK_SET:从文件开头开始计算偏移(offset >= 0)。
SEEK_CUR:从当前文件位置开始计算偏移(可正可负)。
SEEK_END:从文件末尾开始计算偏移(可正可负,常用于扩展文件)
返回值:
成功:返回新的文件偏移量(从文件开头计算的字节数)。
失败:返回 -1,错误码保存在 errno 中
*/
程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
//
int test1()
{
int fd = 0;
off_t len = 0;
char ch = 0;
ssize_t num;
char buf[64] ;
//读写打开
fd = open("./file2.txt", O_RDWR);
if (-1 == fd)
{
perror("fail to open file ");
return -1;
}
num = read(fd, buf, sizeof(buf));
printf("旧文件read size = %d\n", num);//读取字节大小
printf("旧文件read buf = %s\n", buf);
//从开头向后偏移10个位置
len = lseek(fd, 10, SEEK_SET);
printf("偏移量:%ld 写入!\n", len);
ch = '!';
write(fd, &ch, 1);//在该位置写入一个字符
//从当前位置向前偏移5个位置
len = lseek(fd, -5, SEEK_CUR);
printf("偏移量:%ld 写入@\n", len);
ch = '@';
write(fd, &ch, 1);
len = lseek(fd, -2, SEEK_END);
printf("偏移量:%ld 写入#\n", len);
ch = '#';
write(fd, &ch, 1);
//将偏移量设置到文件开头
len = lseek(fd, 0, SEEK_SET);
printf("偏移量:%ld\n", len);
num = read(fd, buf, sizeof(buf));
if( num == -1)
{
printf(" fail to read \n");
return -1;
}
else if( num == 0)
{
printf(" read end \n");
return 0;
}
printf("新文件read size = %d\n", num);//读取字节大小
printf("新文件read buf = %s\n", buf);
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
test1();
return 0;
}
运行结果:原文件内容
在不同偏移量写入数据