最近在学习Linux C 文件读写,实现功能:
- 输入源文件,目标文件,完成copy功能;
- 输入一个文件,显示文件大小;
内容如下,还是比较顺利的
IO_File.c
#include "IO_file.h"
char write_buff[100] = {0};
off_t Get_file_size(char *filepath)
{
int file_fd = 0;
off_t File_size = 0;
file_fd = open(filepath,O_RDONLY,FILE_CHMOD);
if(file_fd == -1)
{
printf("file open failed\r\n");
return -1;
}
else
{
printf("file open success\r\n");
}
File_size = lseek(file_fd, 0, SEEK_END);
close(file_fd);
return File_size;
}
int File_Copy(char *Src,char *dst)
{
int Src_file_fd = 0;
int dst_file_fd = 0;
ssize_t write_ret = 0;
off_t lseek_ret = 0;
off_t SrcFile_size = 0;
ssize_t read_ret = 0;
int read_count = 0;
Src_file_fd = open(Src,O_RDONLY,S_IRUSR||S_IRGRP|S_IROTH);//只读权限,如果文件不存在报错
if(Src_file_fd == -1)
{
printf("open Src file failed\r\n");
return -1;
}
else
{
printf("open Src file success\r\n");
}
dst_file_fd = open(dst,O_RDWR|O_CREAT,FILE_CHMOD);//如果文件不存在则创建文件,读写权限
if(dst_file_fd == -1)
{
printf("open dst file failed\r\n");
return -1;
}
else
{
printf("open dst file success\r\n");
}
// 查询文件大小
SrcFile_size = lseek(Src_file_fd, 0, SEEK_END);
if(SrcFile_size == -1)
{
printf("Check file size err \r\n");
close(Src_file_fd);
}
else
{
printf("file sizes %ld\r\n",SrcFile_size);
}
//将源文件和目标文件的指针都放在文件头部
lseek_ret = lseek(Src_file_fd, 0, SEEK_SET);
if(lseek_ret == -1)
{
printf("Src_file lseek err \r\n");
close(Src_file_fd);
}
else
{
printf("Src_file lseek success %ld\r\n",lseek_ret);
}
lseek_ret = lseek(dst_file_fd, 0, SEEK_SET);
if(lseek_ret == -1)
{
printf("dst_file lseek err \r\n");
close(dst_file_fd);
}
else
{
printf("dst_file lseek success %ld\r\n",lseek_ret);
}
while(read_count <= SrcFile_size)
{
read_ret = read(Src_file_fd, read_buf, sizeof(read_buf));//返回值:成功->读取的字节数,失败- -1
if(read_ret == -1)
{
printf("src file read failed:%s\r\n",read_buf);
break;
}
write_ret = write(dst_file_fd, read_buf, read_ret);
if(write_ret != -1)
{
printf("dst file write success\r\n");
}
else
{
printf("dat file write err\r\n");
}
memset(read_buf,0,sizeof(read_buf));
read_count += sizeof(read_buf);
}
close(Src_file_fd);
close(dst_file_fd);
}
IO_File.h
#ifndef __IO_FILE_H
#define __IO_FILE_H
#include "main.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void Config_file_init(void);
int File_Copy(char *Src,char *dst);
off_t Get_file_size(char *filepath);
#endif
main.h
#ifndef __MAIN_H__
#define __MAIN_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#endif
main.c
#include "main.h"
#include "IO_file.h"
//argc 是argument count的缩写表示传入main函数中的参数个数,包括这个程序本身
//argv 是 argument vector的缩写表示传入main函数中的参数列表,其中argv[0]表示这个程序的名字
//通常都用argc argv来表示,*argv[]其实就是一个字符类型的数组,可以理解为对象语言中的string类型的数组,也可以写成**agrv
int main(int argc,char *agrv[])
{
off_t ret = 0;
char Src_file[100] = {0};
char dst_file[100] = {0};
//打印入参
for(int i=0;i<argc;i++)
{
printf("%d %s\r\n",i,agrv[i]);
}
if(argc == 3)
{
memcpy(Src_file,agrv[1],strlen(agrv[1]));
memcpy(dst_file,agrv[2],strlen(agrv[2]));
printf("%s %s \r\n",Src_file,dst_file);
File_Copy(Src_file,dst_file);
}
else if(argc == 2)
{
memcpy(Src_file,agrv[1],strlen(agrv[1]));
printf("%s \r\n",Src_file);
ret = Get_file_size(Src_file);
if(ret == -1)
{
printf("Get %s size err\r\n",Src_file);
}
else
{
printf("%s file size:%ld\r\n",Src_file,ret);
}
}
return 0;
}
源文件
Only when you understand the true meaning of life can you live truly.
Bittersweet as life is, it's still wonderful,
and it's fascinating even in tragedy.
If you're just alive, try harder and try to live wonderfully.
Ubuntu运行如下
vmuser@ubuntu:/mnt/hgfs/share/Study_Proj$ gcc main.c IO_file.c -o num01.out
vmuser@ubuntu:/mnt/hgfs/share/Study_Proj$ ./num01.out config_file.txt dst_config.txt
0 ./num01.out
1 config_file.txt
2 dst_config.txt
config_file.txt dst_config.txt
open Src file success
open dst file success
file sizes 223
Src_file lseek success 0
dst_file lseek success 0
dst file write success
dst file write success
dst file write success
通过以上运行可以看出 ./num01.out 属于第一个入参
此时打开目标文件,复制成功
vmuser@ubuntu:/mnt/hgfs/share/Study_Proj$ ./num01.out config_file.txt
0 ./num01.out
1 config_file.txt
config_file.txt
file open success 0644
config_file.txt file size:223
vmuser@ubuntu:/mnt/hgfs/share/Study_Proj$ ./num01.out dst_config.txt
0 ./num01.out
1 dst_config.txt
dst_config.txt
file open success 0644
dst_config.txt file size:223
vmuser@ubuntu:/mnt/hgfs/share/Study_Proj$
通过指令查看文件大小,发现源文件与目标文件大小相同。
只有在你了解了人生的真谛后,才能真正地生活。虽然人生苦忧参半,但依旧美妙,而且即使在悲剧中也藏着迷人之处。如果你只是活着,那就再努力点吧,试着活得精彩。