Thread: breaking a big file into smaller files

  1. #1
    Registered User
    Join Date
    Jul 2006

    Question breaking a big file into smaller files

    i want to break a large file into smaller files each of size 2KB.i want to give the file names as 1.txt,2.txt,3.txt etc how can i do this

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Portland, Or
    read in some of the file... then write out to a file... then read in more of the file... and write more... till you are done reading.

  3. #3
    Registered User
    Join Date
    Jul 2006
    problem is how to name the files i have an index variable i want to create the files with the name as that of the contents of index variable

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    variable i=1;
    char tempname[6];
    read base file till end
    read 2kb from file
    write to new file fopen(tempname,"w");
    end read
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I do wish you would stick to one board for your questions

    Fair enough asking on another board if one doesn't respond in a day or two, but to broadcast all over the place just makes it seem like you're trying to post "urgent" without actually saying so.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    My suggestion is to use sprintf to generate a new filename.
    int index = 1; /* this would be your index variable you were talking about */
    char filename[256];
    sprintf(filename, "%d%s", index, ".txt");
    Note that snprintf and a dynamically allocated character array might be a better solution since snprintf returns the characters it would have printed if it had to truncate the buffer. Thus you can use it to size the string correctly, but I didn't want to drag dynamic memory into this.

    [edit]Oh neat... cross-posting.
    Last edited by whiteflags; 07-24-2006 at 12:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 06:04 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM