Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中将一个文件拆分为多个文件?_C - Fatal编程技术网

如何在C语言中将一个文件拆分为多个文件?

如何在C语言中将一个文件拆分为多个文件?,c,C,我正在尝试将一个文件拆分为多个较小的文件。问题是,如果大小(用户提供的)大于缓冲区,程序就会崩溃,否则它工作正常。有人能帮忙吗?代码如下: char * buffer = (char *)malloc(400); FILE *exsistingFile = fopen(filename,"rb"); do { /*reading from a file */ bytesRead = fread( buffer, sizeof( char ), size, ex

我正在尝试将一个文件拆分为多个较小的文件。问题是,如果大小(用户提供的)大于缓冲区,程序就会崩溃,否则它工作正常。有人能帮忙吗?代码如下:

char * buffer = (char *)malloc(400);
FILE *exsistingFile = fopen(filename,"rb"); 

do
{   

    /*reading from a file */    
    bytesRead = fread( buffer, sizeof( char ), size, exsistingFile );

    /*exits if its 0 */ 
    if (bytesRead == 0)
    {
        printf("The reading has finished or the file has no data inside\n");
        return 0;
    } 

    fileCount ++;
    sprintf (newFileName,"%s%04i",output,fileCount);


    /* opening the new file bye the name given by the user */   
    outputFile = fopen(newFileName,"w");



    /*checking whether the file is opened or not*/
     if ( !outputFile )
    {
        printf( "File %s cannot be opened for reading\n", filename );
        return E_BAD_DESTINATION; 
    }

    /*write to file from the buffer*/
    fwrite(buffer,sizeof(char),bytesRead,outputFile);

    /*closing the output file*/
    fclose(outputFile);


    } while ( bytesRead > 0 );

那么,malloc是一个与用户给定大小一样大的缓冲区吗?或者,在达到用户给定的大小之前,只能以400字节的位进行读写

char * buffer = (char *)malloc(400);
FILE *exsistingFile = fopen(filename,"rb"); 

do
{   
    fileCount ++;
    sprintf (newFileName,"%s%04i",output,fileCount);

    /* opening the new file bye the name given by the user */   
    outputFile = fopen(newFileName,"w");

    /*checking whether the file is opened or not*/
    if ( !outputFile )
    {
        printf( "File %s cannot be opened for writing\n", filename );
        return E_BAD_DESTINATION; 
    }

    int workSize = size;
    while (workSize)
    {
        int chunkSize = workSize > 400 ? 400 : workSize;

        /*reading from a file */    
        bytesRead = fread( buffer, sizeof( char ), chunkSize, exsistingFile );
        workSize -= bytesRead;

        /*exits if its 0 */ 
        if (bytesRead == 0)
        {
            printf("The reading has finished or the file has no data inside\n");
            return 0;
        } 

        /*write to file from the buffer*/
        fwrite(buffer,sizeof(char),bytesRead,outputFile);
    }

    /*closing the output file*/
    fclose(outputFile);

} while ( bytesRead > 0 );
差不多吧。也许除了一些虫子,但你明白了。这是家庭作业吗

size_t currentSize = (size >= 400) ? 400 : size;
size -= currentSize;

然后fread only currentSize

如果
大小
是用户输入,则应根据给定输入分配缓冲区,而不是使用硬编码值。

是的,但如果有两个以上的文件,则会有另一个循环写入原因。例如,如果缓冲区为400,文件大小为1000,并且用户想要分割文件450的块。所以应该创建3个文件。那样的话,你会怎么做,你是什么意思?我看不出问题所在。假设要拆分的文件是2000字节。用户已指定大小为500,缓冲区为400。现在我们将大小设置为400,然后它将打开一个文件写入它关闭它,然后它将剩余的100写入另一个文件。我希望它将整个500写入一个文件。是的,我只希望读取和写入400位,但问题是如果该文件被拆分为两个以上的文件。例如,如果缓冲区为400,文件大小为1000,并且用户想要分割文件450的块。所以应该创建3个文件。那样的话,你怎么办?做两个循环。外循环负责创建输出文件,内循环负责读取用户指定数量的数据。这正是困扰我的问题,如果你能尝试修改程序,它会很有帮助,因为我已经尝试过,但也失败了两个循环。很酷,现在我的块大小小于缓冲区大小。但我知道你是怎么做的,很酷。这是由'int chunkSize=workSize>400处理的?400:工作大小;'。。。如果您的区块或区块的剩余部分小于缓冲区大小,它只读取部分。如果用户给出了一个非常大的数字,我的程序将崩溃,因为操作系统不会分配这么大的内存。@Jahirsah:不,您应该测试
malloc
的结果,然后
{perror(“malloc”);退出(exit_FAILURE);}
失败。是的,实际上我必须将缓冲区设置为4096。这是无法改变的。如果用户输入6000,我不能在他身上使程序崩溃。至少对于文本文件,我会逐行循环阅读(例如使用…)。顺便说一句,Linux和Posix具有和命令。您可以使用它们,或者研究它们的源代码,因为它是免费软件。您的open for write失败,因此您会输出一条消息,表示无法读取输入文件??一个简单的修复,使用大小而不是固定值400的malloc缓冲区