本文章主要讲述Linux环境下文件的读写操作,使用到的函数有open、read、write、lseek。
open:用于打开或者创建文件。
read:从文件中读指定字节的数据到内存中。
write:讲内存中数据写入到文件中。
lseek:可以改变当前文件偏移量。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char read_file[] = "/home/imx/shares/open/read.bmp";
char write_file[] = "/home/imx/shares/open/write.bmp";
int file_len = 0;
int file_total = 0;
int read_len = 0;
char buf[1024] = {0};
int read_fd = open(read_file,O_RDWR);
open:用于打开或者创建文件。
read:从文件中读指定字节的数据到内存中。
write:讲内存中数据写入到文件中。
lseek:可以改变当前文件偏移量。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char read_file[] = "/home/imx/shares/open/read.bmp";
char write_file[] = "/home/imx/shares/open/write.bmp";
int file_len = 0;
int file_total = 0;
int read_len = 0;
char buf[1024] = {0};
int read_fd = open(read_file,O_RDWR);