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

  • SEARCH
  • Home
  • 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 7523619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:51:22+00:00 2026-05-30T02:51:22+00:00

Suppose I have a text file that says: This file has plain text Now,

  • 0

Suppose I have a text file that says:
This file has plain text

Now, I want to divide this text file into ‘n’ number of parts with equal characters.
Suppose the user wants three divisions, then 1.txt, 2.txt and 3.txt should be created, with about 8 characters each.

(The next part of this program is to rejoin these files back into the original state but I’m sure I’ll be able to do that myself If I can get help with this first part).

Can you guys guide me in this matter?

Language used is C.

void main(int argc, char** argv)
{
int iChunkNo=0;
char cFileName[30];

iChunkNo = atoi(argv[2]);
strcpy(cFileName, argv[1]);

printf("The file will be divided into %d chunks \n", iChunkNo);

FILE* file_read_pointer;
file_read_pointer = fopen(cFileName, "r");

int iCount=0;
char ch;

while (1) 
{
        ch = fgetc(file_read_pointer);
        if (ch == EOF)
            break;
        ++iCount;
}

printf("The number of characters in the file is: %d \n", iCount);   

int iCharPerFile = 0;
iCharPerFile = iCount/iChunkNo;

printf("The number of characters per chunk file will be: %d \n", iCharPerFile);

FILE* file_write_pointer;   

int j=1;
for(j; j<=iChunkNo; j++)
{   
    char num[5] = {j};
    char ext[4] = {"txt"};

    char name[15];
    sprintf(name, "%d.%s", j, ext);     

    FILE* file_write_pointer;
    file_write_pointer = fopen(name, "w");
}

int i=0;

for(i; i<iCharPerFile; i++)
{
    char temp;
    temp = fgetc(file_read_pointer);
    //fputc(temp, file_write_pointer);
    fprintf(file_write_pointer, "%c", temp);
}
}
  • 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-05-30T02:51:23+00:00Added an answer on May 30, 2026 at 2:51 am

    You could do something like this:

    • Find the size of the file (using fseek and ftell). Then seek back to the beginning
    • Determine the size you need to write to each file (size / n)
    • Read one character at a time (use getc) and write to the corresponding file (putc)
      • Use sprintf(fname, "%d.txt", index) to build names like “1.txt”
      • Use fopen to open files and keep a FILE *current to which you write at each step

    That being said, you should start slow. First make a program that simply copies one file into another using getc + putc and work your way up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a text file that looks like this: 33 3 46 12
I run across this problem frequently suppose I have a text file that I
Suppose I have a text file with data separated by whitespace into columns. I
Suppose I have a text file with some data I want to retrieve lost
Suppose I have a string such as this in a text file: (((var1 AND
Suppose I have a text that I can easily parse. It consists of text
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
We have a command line exe that takes input from a text file and
I have a text file that is almost sometimes 5 mb large. What I

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.