breaking a big file into smaller files

This is a discussion on breaking a big file into smaller files within the C Programming forums, part of the General Programming Boards category; i want to break a large file into smaller files each of size 2KB.i want to give the file names ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    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
    Location
    Portland, Or
    Posts
    1,094
    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
    Posts
    3
    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
    Location
    India
    Posts
    167
    variable i=1;
    char tempname[6];
    read base file till end
    read 2kb from file
    tempname=strcat(inttostring(i)+".txt");
    write to new file fopen(tempname,"w");
    i++;
    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
    for"

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    I do wish you would stick to one board for your questions
    http://forums.devshed.com/c-programm...ng-370788.html

    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    My suggestion is to use sprintf to generate a new filename.
    Code:
    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, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21