www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/30/18:30:46

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How to get file size?
Date: Mon, 29 Sep 1997 13:40:13 -0700
Organization: Alcyone Systems
Lines: 31
Message-ID: <3430122D.798B8271@alcyone.com>
References: <01bccc5b$8138ba00$0200a8c0 AT ingo>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Ingo Ruhnke wrote:

> This returns me the number of bytes of the file, but the number of
> chars is
> a little bit smaller, because of the <LF><CR>.
> So is there a standart way to get the number of chars in a file?

There unfortunately is no standard, easy way to get the number of
characters in a file, particularly if you're talking about an ASCII
file.  The usual way for binary files is to seek to the end of the file,
then do ftell, but unfortunately 1. the SEEK_END seek mode isn't
guaranteed to be supported by ANSI C (dumb), and 2. for text files ftell
is not guaranteed to give you the file size, with or without newline
translation (understandable, but they could have been more specific
about it).

The simplest, though slowest way, is to just make a first pass through
the file, counting characters and newlines.  Ugly, but it's the only
standard, surefire way to do it.

Then again, there's the argument that if you have to read in the whole
file into memory at ocne, there's probably a better solution to your
problem than doing so.

-- 
          Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                        Alcyone Systems / http://www.alcyone.com/max/
   San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                      \
   "After each war there is a little / less democracy to save."
                                    / Brooks Atkinson

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019