[comp.sys.ibm.pc] Turbo C file size routine

alang@masscomp.UUCP (Alan Groupe) (10/30/87)

I'm looking for a way to determine the size of a text file from within Turbo
C. Determining the size of a binary file is trivial, using the stat call,
but if I open the file in text mode, stat returns the same value, which
is now incorrect (ie. CR/LF count as two characters in the count, although
are read as a single newline character. So far, I've been opening the file
in text mode and running the following loop, but this is slow:

	i=0;
	while (getc(fp) >= 0) i++;

Is there a faster way to do this?

Alan Groupe

ks26+@andrew.cmu.edu (Kenneth Sykes) (11/02/87)

>I'm looking for a way to determine the size of a text file from within Turbo
>C. Determining the size of a binary file is trivial, using the stat call,
>but if I open the file in text mode, stat returns the same value, which
>is now incorrect (ie. CR/LF count as two characters in the count, although
>are read as a single newline character. So far, I've been opening the file
>in text mode and running the following loop, but this is slow:
>
>	i=0;
>	while (getc(fp) >= 0) i++;
>
>Is there a faster way to do this?

Assuming you want the file size as indicated by the DIR command, there
 are two approaches you can take:

1)  If you don't mind opening the file, then use the following code:
             #include <io.h>

               int fh;
               fh = _open(file, 0);
               <the file size> =  filesize(fh);
               _close(fh);

2) Otherwise, use the findfirst call:
            #include <dir.h>

            struct ffblk blk;

             if ( findfirst(file, <attributes>, &blk) )
		<file not found>;
             else
                      <the file size> = blk.ff_fsize;

Since I am pulling the syntax off the top of my head, check the Reference
manual to make sure I have the correct calling sequence, field names, etc.

-- Ken Sykes

nez@bucc2.UUCP (11/03/87)

/* Written by masscomp.UUCP!alang in comp.sys.ibm.pc */
> I'm looking for a way to determine the size of a text file from within Turbo
> C. Determining the size of a binary file is trivial, using the stat call,
> but if I open the file in text mode, stat returns the same value, which
> is now incorrect (ie. CR/LF count as two characters in the count, although
> are read as a single newline character. So far, I've been opening the file
> in text mode and running the following loop, but this is slow:
> 
> i=0;
> while (getc(fp) >= 0) i++;
> 
> Is there a faster way to do this?
> 
> Alan Groupe
/* End of text from bucc2:comp.sys.ibm.pc */

	How about trying something like:

--------
|	#define MAXSIZE 512
|
|	char temp_buf[MAXSIZE];
|
|	.
|	.
|	.
|
|	i=0;
|	while( (i+=read(fp,temp_buf,MAXSIZE) )>0);
---------

	According to the Turbo C Reference manual, the function 'read'
returns the number of bytes read and doesn't count ^Zs or ^Ms.

	Hope this helps...


                           Rich Neswold

         =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                                 / bucc2!nez
               ...!ihnp4!bradley!- cyber!xx64194
                                 \ buee730!nez
         =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

	         "It's a dirty, sadistic job -
		  but somebody gets to do it."

bill@trotter.usma.edu (Bill Gunshannon) (11/06/87)

In article <32100013@bucc2>, nez@bucc2.UUCP writes:
> 
> /* Written by masscomp.UUCP!alang in comp.sys.ibm.pc */
> > I'm looking for a way to determine the size of a text file from within Turbo
> > C.
> > 
> > i=0;
> > while (getc(fp) >= 0) i++;
> > 
> > Is there a faster way to do this?
> > 
> /* End of text from bucc2:comp.sys.ibm.pc */
> 
> 	How about trying something like:
EXAMPLE USING read() DELETED FOR BREVITY
> 	According to the Turbo C Reference manual, the function 'read'
> returns the number of bytes read and doesn't count ^Zs or ^Ms.
> 


Am I missing something here?   

Why not just use filelength()?

And of course if you find that too boring there is another method:

    FILE    *F0;
    long    file_length, ftell();
    int     fseek();

    fseek(F0,0L,SEEK_END);
    file_length = ftell(F0);


I guess it just depends on how hard you want to make things.  Another point
in favor of the second method is it should be portable as fseek() and ftell()
are standard routines and filelength() is not.

Hope this helps.

bill gunshannon


UUCP: {philabs}\		 	US SNAIL: Martin Marietta Data Systems 
      {phri   } >!trotter.usma.edu!bill           USMA, Bldg 600, Room 26 
      {sunybcs}/			          West Point, NY  10996	     
RADIO:         KB3YV		        PHONE: WORK    (914)446-7747
AX.25:         KB3YV @ K3RLI	        PHONE: HOME    (914)565-5256

alang@masscomp.UUCP (Alan Groupe) (11/09/87)

 bill@trotter.usma.edu (Bill Gunshannon) writes:
>
>Am I missing something here?   
>
>Why not just use filelength()?
>
>And of course if you find that too boring there is another method:
>
Yes Bill, I think you are missing something. Consider the following
small file:

a
b

What is the length of this file? Is it 6 bytes long (a<CR><LF>b<CR><LF>)
or is it 4 bytes long (a\nb\n)? The answer is: both. If the file is
opened in binary mode, you can read 6 characters from it. If it is
opened in text mode, you can read just four, as the TC runtime code
converts the <CR><LF> pair into a newline. I want to get back the 4,
but stat()/fstat()/filelength()/fseek()/ftell()/lseek() etc. will all
return 6, since they simply ask DOS for the file length. The original
posting asked if there were some manner of doing this other than simply
reading sequentially through the file. From the responses, it does not
appear that anyone was able to figure out any other way, although the
comment about doing larger reads is well taken.

Alan Groupe

tr@wind.bellcore.com (tom reingold) (11/16/87)

In article <1005@trotter.usma.edu> bill@trotter.usma.edu (Bill Gunshannon) writes:
$ [...]
$ Am I missing something here?   
$ 
$ Why not just use filelength()?
$ [...]

Yes, you missed something because people have edited away the
essential part of the poster's question.  He wants to know what the
file length would be if it were on a Unix system.  That is to say, the
CR/LF combinations on MS-DOS become only LF on Unix so the character
count changes.  Now, can you obtain the effective size of a file that
way???  I don't think so.  I think that his method,

  while (getc(fp) >= 0) 
	char_count++;

may in fact, be the only way.  After all, how can you tell how many
CR/LFs are in the file without reading them?  Remember, most input
routines in PC C compilers treat CR/LF's as a LF when the file is
opened in text mode and thus the character count is different than the
file size.

Tom Reingold                    INTERNET:       tr@bellcore.bellcore.com
Bell Communications Research    UUCP:           <backbone>!bellcore!tr
435 South St room 2L350         SOUNDNET:       (201) 829-4622 [work]
Morristown, NJ 07960                            (201) 287-2345 [home]