Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:53:19+00:00 2026-06-09T08:53:19+00:00

I program a program to split file in C in Ubuntu. I have error

  • 0

I program a program to split file in C in Ubuntu. I have error when get buffer in readfile.

Here is my code:

    int split(char *filename, unsigned long part) {
        FILE *fp;
        char *buffer;
        size_t result; // bytes read
        off_t fileSize;
    
        fp = fopen(filename, "rb");
        if (fp == NULL) {
            fprintf(stderr, "Cannot Open %s", filename);
            exit(2);
        }
    // Get Size
        fileSize = get_file_size(filename);
    // Buffer
        buffer = (char*) malloc(sizeof(char) * (fileSize + 1));
        if (buffer == NULL) {
            fputs("Memory error", stderr);
            fclose(fp);
            return 1;
        }
    // Copy file into buffer
    //char buffers[11];
        result = fread(buffer, 1, fileSize, fp);
        buffer[fileSize] = '\0';
    
        if (result != fileSize) {
            fputs("Reading error", stderr);
            return 1;
        }
    
    // Split file
        off_t partSize = fileSize / part;
    // Last Part
        off_t lastPartSize = fileSize - partSize * part;
        unsigned long i;
        unsigned long j;
        // create part 1 to n-1
        for (j = 0; j < part; j++) {
            char partName[255];
            char *content;
            char partNumber[3];
            // Content of file part
    //      for (i = j; i < partSize * (j + 1); i++) {
    //
    //      }
            content = (char*) malloc(sizeof(char) * partSize);
            content = copychar(buffer, j + i, partSize + i);
            i += partSize;
            //copy name
            strcpy(partName, filename);
            // part Number
            sprintf(partNumber, "%d", j);
            // file name with .part1 2 3 4 ....
            strcat(partName, ".part");
            strcat(partName, partNumber);
            // Write to file
            writeFile(partName, content);
            free(content);
        }
    // last part
    char *content;
    content = (char*) malloc(sizeof(char) * (fileSize - partSize * (part - 1)));
    content = copychar(buffer, (part - 1) * partSize + 1, fileSize);
    char lastPartNumber[3];
    char lastPartName[255];
    sprintf(lastPartNumber, "%d", part);
    strcpy(lastPartName, filename);
    strcat(lastPartName, ".part");
    strcat(lastPartName, lastPartNumber);
    writeFile(lastPartName, content);
    free(content);

    free(buffer);
    fclose(fp);
    return 0;    
}

Here is function copychar from start to end:

char *copychar(char* buffer, unsigned long start, unsigned long end) {
    if (start >= end)
        return NULL;
    char *result;
    result = (char*) malloc(sizeof(char) * (end - start) + 1);
    unsigned long i;
    for (i = start; i <= end; i++)
        result[i] = buffer[i];
    result[end] = '\0';
    return result;
}

Here is function to get filesize:

off_t get_file_size(char *filename) {
    struct stat st;
    if (stat(filename, &st) == 0)
        return st.st_size;
    fprintf(stderr, "Cannot determine size of %s: %s\n", filename);
    return -1;
}

Here is function to write file:

    int writeFile(char* filename, char*buffer) {
    if (buffer == NULL || filename == NULL)
        return 1;
    FILE *file;
    file = fopen(filename, "wb");
    fwrite(buffer, sizeof(char), sizeof(buffer) + 1, file);
    fclose(file);
    return 0;
}

When I test I use file test 29MB and it dumped. I debug and it returned fileSize true but when readfile in buffer get from file it only return 135 characters and when use copychar it error.

    Breakpoint 1, 0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary   1342169369  0     0     100644  4         `\n2.0\ncontrol.tar.gz  1342169369  0     0     100644  4557      `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary   1342169369  0     0     100644  4         `\n2.0\ncontrol.tar.gz  1342169369  0     0     100644  4557      `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

How can I divide buffer into part to write into part when split?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:53:20+00:00Added an answer on June 9, 2026 at 8:53 am

    It’s highly impractical to copy files in 1 big block as you may have noticed. And it’s not needed.

    At the simplest level you could copy the file byte by byte, like this

    while( ( ch = fgetc(source) ) != EOF ) {
       fputc(ch, target);
    }
    

    Which will work, but it will be quite slow. Better to copy in blocks, like this:

     unsigned char buf[4096];
     size_t size;
     while( (size = fread(buf, 1, sizeof(buf), fpRead) ) > 0) {
         fwrite(buf, 1, size, fpWrite);
     }
    

    Notice that the resulting code is way simpler and contains no dynamic memory allocation.

    You still need to add the splitting logic of course, but that can be done by tracking the number of bytes written and opening a new write-file before actually writing it.

    EDIT: how to handle the multipart facet – schematically, you still need to implement extra checks for some special cases and test results of the different system calls of course

     unsigned char buf[4096];
     size_t size;
     size_t partsize = 100000; // asssuming you want to write 100k parts.
     size_t stilltobewritten = partsize; // bytes remaining to be written in current part
     size_t chunksize = sizeof(buf); // first time around we read full buffersize
     while( (size = fread(buf, 1, chunksize, fpRead) ) > 0) {
         fwrite(buf, 1, size, fpWrite);
         stilltobewritten -= size; // subtract bytes written from saldo
         if (stilltobewritten == 0) {
             // part is complete, close this part and open next
             fclose(fpWrite);
             fpWrite = fopen(nextpart,"wb");
             // and reinit variables
             stilltobewritten = partsize;
             chunksize = sizeof(buf);
         } else {
             // prep next round on present file - just the special case of the last block
             // to handle
             chunksize = (stilltobewritten > sizeof(buf)) ? sizeof(buf) : stilltobewritten;
         }
     }
    

    and EDIT 2: the file part name can be made a LOT simpler as well:

     sprintf(partName, "%s.part%d",file, j);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that describes input data, which is split into several other
I'm using the os.path.split() function on a path in my program to get the
I have a program that load data from a file using std::ifstream and store
Here is a small snippet from a C# program to generate dictionary from file.
Say I have a 5 GB file. I want to split it in the
I have a mapreduce job whose role is to split my input file into
I have a program which reads a text file and processes it to be
I have a program which processes a text log file and tokenizes the text
So I've written some code in Ruby to split a text file up into
In My program I have a file and then I read through all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.