Truncating large text files in C 
Author Message
 Truncating large text files in C

I have several 7Mb text files and I need to truncate the first 5.6Mb so that
the last part fits on a single floppy disk.

Can anybody give a any clues on how to achive this (I am a C newbie, so keep
it simplish).



Mon, 20 Aug 2001 03:00:00 GMT  
 Truncating large text files in C


Quote:
> I have several 7Mb text files and I need to truncate the first 5.6Mb so
that
> the last part fits on a single floppy disk.

> Can anybody give a any clues on how to achive this (I am a C newbie, so
keep
> it simplish).

Before I show you the code, some caveats:

1) because there is a danger that this is a disguised homework question,
I've written the code in pretty poor style. It's a hack, not something
you'd hand in. If this isn't a homework question, it'll be good enough for
rock n' roll.

2) I haven't compiled and tested this program!!! This is your
responsibility. There may be a bug in there. Or even two or three. But at
least the input file is not over-written, so you have scope to test safely.

3) You should add error-checking of all functions for full robustness. If
you don't, it will still probably work just fine.

4) It may be that on your system, newlines are made up of two characters,
0x0d and 0x0a. If so, there is a small but significant chance that the
first character read may be a 0x0a character. This might{*filter*}up some text
editors, so you might want to add code to test for this, just in case (it's
not hard to do).

5) Before using this code, wait at least a day, then check back here again,
in case one of the local experts has spotted some glaring flaw in the code.

Okay, here we go:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *filename = "myfile.txt"; /* make sure this is in the current
working directory where the program is built. There are better ways to do
this, I'm keeping it simple. Obviously, change it to the actual filename if
necessary. */

    char *outfile = "myfile.out"; /* this is where the output will be
stored */
    long infilesize = 7000000; /* replace this with the actual size of the
file (use dir or ls or whatever your system uses to display file info) */

    long outfilesize = 1400000; /* you might want to adjust this slightly
for the exact size of your floppy disk */

    FILE *fpin, *fpout;
    char buffer[512];   /* you might want to increase this buffer size - up
to you. */

    fpin = fopen(filename, "rb");
    if(fpin == NULL)
        return EXIT_FAILURE;
    fpout = fopen(outfile, "wb"); /* warning - will overwrite any file with
the name myfile.out */
    if(fpout == NULL)
    {
        fclose(fpin);
        return EXIT_FAILURE;
    }

    fseek(fpin, infilesize - outfilesize, SEEK_SET);

    while(bytesread = fread(buffer, 1, sizeof buffer, fpin) > 0)
    {
        fwrite(fpout, 1, sizeof buffer, fpout);
    }
    fclose(fpin);
    fclose(fpout);
    return EXIT_SUCCESS;

Quote:
}

--
Richard H

#include "sig.h"



Mon, 20 Aug 2001 03:00:00 GMT  
 Truncating large text files in C
: I have several 7Mb text files and I need to truncate the first 5.6Mb so that
: the last part fits on a single floppy disk.

: Can anybody give a any clues on how to achive this (I am a C newbie, so keep
: it simplish).

#include <stdio.h>

int main(int argc, char *argv[])
{  
  int ch;
  unsigned long count;
  FILE *fpin, *fpout;
  if (argc < 3)
        return 0;
  if ((fpin = fopen(argv[1], "rb")) == NULL)
        return 0;
  if ((fpout = fopen(argv[2], "wb")) == NULL)
        return 0;
  for (count = 0 ; count < 5600000 ; count++)
        ch = getc(fp);
  while ((ch = getc(fp)) != EOF)
        (void) putc(ch, fpout);
  fclose(fpin);
  fclose(fpout);
  return 0;

Quote:
}

Simple, slow, and lacks error checking; but just about useable
as a throw-away.  It would be easier if shells didn't mostly
set their i/o streams to text by default.

Will



Mon, 20 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. TRUNCATING OPENED TEXT FILE

3. Append from text file & Truncate Table

4. Easy way to truncate a text file

5. how to truncate a text file

6. how to truncate a file opened in text mode

7. Creating separate text files from one large one

8. Dealing with large text files

9. Help - Insertion in large text files

10. Sorting strings from a large text file

11. Help: Need to display a large text file

12. How to read large text file?

 

 
Powered by phpBB® Forum Software