码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-09-03 00:21:26      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:class   none   wro   span   play   cut   分享   open   std   

1.通过c直接实现

技术分享
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int fileNum=0;
char fileNam[10];

char fileName(){
    fileNam[10]=0;
    fileNum += 1;
    char str[4];
    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[245120];
    int n;
    
    if((fd_src=open("./raw.data",O_RDONLY))<0)
    {
        perror("open src");
        exit(EXIT_FAILURE);
    }
    
    while((n=read(fd_src,buf,sizeof(buf)))>0){
        if (n != 245120)
        {
            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,0666)<0)
        {
            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);    
}
View Code

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

技术分享
#! /bin/sh 

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

 

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

标签:class   none   wro   span   play   cut   分享   open   std   

原文地址:http://www.cnblogs.com/Malphite/p/7468293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!