此文探讨文件操作函数的简单应用,涉及主要函数:open,write,read,lseek,close,等
demo1:
linux操作系统简单读写交互
1.linux界面输入hello
2.linux界面打印出hello
要求:使用read,write函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void result()
{
int fd_read;
int fd_write;
char buf[20];
fd_read = read(0,buf,strlen(buf));
if(fd_read == -1)
{
printf("read fail\n");
perror("why");
exit(-1);
}
fd_write = write(1,buf,strlen(buf));
if(fd_write == -1)
{
printf("write fail\n");
perror("why");
exit(-1);
}
}
int main()
{
result();
return 0;
}
linux操作系统中,0对应输入,1对应输出
demo2:
创建文件写入其他类型:
1.创建文件
2.写入int类型(或结构体类型)
3.读取输入类型值
int类型示例:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
void result()
{
umask(0);
int fd;
int write_buf = 10;
int fd_write;
int fd_read;
int fd_lseek;
int read_buf;
int fd_close;
fd = open("/root/pro/file",O_CREAT|O_RDWR,0777);
if(fd == -1)
{
printf("create file fail\n");
perror("why");
exit(-1);
}
fd_write = write(fd,&write_buf,sizeof(write_buf));
if(fd_write == -1)
{
printf("write file fail\n");
perror("why");
exit(-1);
}
fd_lseek = lseek(fd,0,SEEK_SET);
if(fd_lseek == -1)
{
printf("lseek fail\n");
perror("why");
exit(-1);
}
fd_read = read(fd,&read_buf,sizeof(write_buf));
if(fd_read == -1)
{
printf("read file fail\n");
perror("why");
exit(-1);
}
printf("read context : %d\n",read_buf);
fd_close = close(fd);
if(fd_close == -1)
{
printf("close file fail\n");
perror("why");
exit(-1);
}
}
int main()
{
result();
return 0;
}
结构体类型示例:
要求:外部传参,参数为结构体数据
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person
{
char name[20];
int num;
};
void result(char name[],int num)
{
umask(0);
int fd;
struct person write_buf;
strcpy(write_buf.name,name);
write_buf.num = num;
int fd_write;
int fd_read;
int fd_lseek;
struct person read_buf;
int fd_close;
fd = open("/root/pro/file",O_CREAT|O_RDWR,0777);
if(fd == -1)
{
printf("create file fail\n");
perror("why");
exit(-1);
}
fd_write = write(fd,&write_buf,sizeof(struct person));
if(fd_write == -1)
{
printf("write file fail\n");
perror("why");
exit(-1);
}
fd_lseek = lseek(fd,0,SEEK_SET);
if(fd_lseek == -1)
{
printf("lseek fail\n");
perror("why");
exit(-1);
}
fd_read = read(fd,&read_buf,sizeof(struct person));
if(fd_read == -1)
{
printf("read file fail\n");
perror("why");
exit(-1);
}
printf("read context : name : %s num : %d\n",read_buf.name,read_buf.num);
fd_close = close(fd);
if(fd_close == -1)
{
printf("close file fail\n");
perror("why");
exit(-1);
}
}
int main(int argc,char **argv)
{
printf("example : ./program name numder\n");
if(argc != 3)
{
printf("param is error\n");
exit(-1);
}
result(argv[1],atoi(argv[2]));
return 0;
}
结果示例:
mian函数传递的参数均为字符串类型,如需要其他类型参数,需对数据类型做转换
demo2:
简单的linux命令cp:
1.创建文件file
2.file中写入hello word
3.创建文件bak
4.读取file内容,写入到bak中
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void result(char file[],char bak[])
{
int fd_file;
int file_size;
char *file_rbuf;
int fd_bak;
fd_file = open(file,O_RDWR);
file_size = lseek(fd_file,0,SEEK_END);
lseek(fd_file,0,SEEK_SET);
file_rbuf = (char *)malloc(file_size);
read(fd_file,file_rbuf,file_size);
fd_bak = open(bak,O_CREAT|O_RDWR,0777);
write(fd_bak,file_rbuf,file_size);
close(fd_file);
close(fd_bak);
}
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("param is error\n");
perror("why");
exit(-1);
}
result(argv[1],argv[2]);
return 0;
}
结构示例:
demo3:
config文件修改:
1.创建config文件
2.写入一下内容:
num : 1
name : kaxi
socer : 59
3.修改59位99
strstr:字符串查
char *strstr(const char *haystack, const char *needle);
haystack:被查找字符串
needle:查找字符串
返回值
成功执行返回第一次出现字符串的位置,错误返回NULL
结构示例:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void result(char file[],char *data)
{
int fd;
int file_size;
char *fd_buf;
fd = open(file,O_RDWR);
file_size = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
fd_buf = (char *)malloc(file_size);
read(fd,fd_buf,file_size);
char *p = strstr(fd_buf,"socer : ");
if(p == NULL)
{
printf("not found\n");
exit(-1);
}
else
{
p = p + strlen("socer : ");
*p = *data;
}
lseek(fd,0,SEEK_SET);
write(fd,fd_buf,strlen(fd_buf));
close(fd);
}
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("param is error\n");
exit(-1);
}
result(argv[1],argv[2]);
return 0;
}