实现功能:将配置文件里面的字符串改为逆序;(例如在TEXT.config 里面写入“abcdefg”,当运行完程序之后TEXT.config 里面变为“gfedcba”)
目的:进一步锻炼linux文件I/O编程基本函数的使用:open 、read、write、close、lseek;
LinuxC下的字符串strstr函数的进一步巩固使用;
函数的使用功能:https://blog.csdn.net/qq_41899773/article/details/88824897
该总结是基于(总结二)跟(总结三)结合C语言的数组遍历实现的,若有疑惑可查看一下
代码实现:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int fds;
int fdd;
char Buf[10] = {0};
char readBuf[10] = {0};
int i;
fds = open(argv[1],O_RDWR);
read(fds,Buf,10);
lseek(fds,0,SEEK_SET);
fdd = open(argv[1],O_RDWR,0600);
int len = strlen(Buf);
printf("buf=%s\n",Buf);
for(i=0;i<len-1;i++){
printf("the src arry is:%c\n",Buf[i]);
readBuf[i] = Buf[6-i];
}
printf("the goal arry is:%s\n",readBuf);
lseek(fdd,0,SEEK_SET);
write(fdd,readBuf,strlen(readBuf));
close(fds);
close(fdd);
return 0;
}
~
~
~
~
运行结果显示:![](https://i-blog.csdnimg.cn/blog_migrate/5561fb17bfc8a1bf77631af7e4c7893b.png)