linux 将一个文件分解成多个不同名文件

时间:2023-03-08 17:00:30
linux 将一个文件分解成多个不同名文件

1.通过c直接实现

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int fileNum=;
char fileNam[]; char fileName(){
fileNam[]=;
fileNum += ;
char str[];
char s1[]="./image/";
char s2[]=".yuv";
sprintf(str,"%d",fileNum);
sprintf(fileNam,"%s%s%s",s1,str,s2);
} int main(int argc,char* argv[])
{
int fd_src,fd_dst;
char buf[];
int n; if((fd_src=open("./raw.data",O_RDONLY))<)
{
perror("open src");
exit(EXIT_FAILURE);
} while((n=read(fd_src,buf,sizeof(buf)))>){
if (n != )
{
perror("file size small");
close(fd_src);
exit(EXIT_FAILURE);
} //lseek(fd_src,245120,SEEK_CUR);
fileName(); if (fd_dst = open(fileNam,O_CREAT|O_WRONLY,)<)
{
perror("open dst fail");
close(fd_src);
exit(EXIT_FAILURE);
} write(fd_dst,buf,n); printf("copy successfully\n");
close(fd_dst);
system("./move.sh");
}
printf("copy successfully\n");
close(fd_src);
exit(EXIT_SUCCESS);
}

2.通过调用system函数执行脚本

#! /bin/sh 

file_name(){
rm ./image/.txt
touch ./image/.txt
chmod ./image/.txt
ls -l image | cut -d ' ' -f | cut -d '.' -f > ./image/.txt
num=$(tail ./image/.txt -n )
echo "$num"
num=$(($num+))
echo "$num"
}
move (){
file_name
mv ./image/.yuv ./image/"$num".yuv
}
move