Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72272

Re: Working with files on different operating systems

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Working with files on different operating systems
Date 2015-10-09 09:16 -0700
Organization None to speak of
Message-ID <ln1td4ggp5.fsf@kst-u.example.com> (permalink)
References <mv7b7l$gru$1@speranza.aioe.org> <mv7id4$duj$1@dont-email.me>

Show all headers | View raw


Les Cargill <lcargill99@comcast.com> writes:
> Morten W. Petersen wrote:
>> I was working on removing warnings from 'gcc -Wall....' and found that
>> fileno was a POSIX-specific function call,
>
> ???
>
> For Mingw:
>
> C:\windows\system32>grep fileno \MinGW\include\*.h
> \MinGW\include\stdio.h:_CRTIMP int __cdecl __MINGW_NOTHROW      _fileno 
> (FILE*);
> \MinGW\include\stdio.h:_CRTIMP int __cdecl __MINGW_NOTHROW      fileno 
> (FILE*);
> \MinGW\include\stdio.h:#define _fileno(__F) ((__F)->_file)
> \MinGW\include\stdio.h:#define fileno(__F) ((__F)->_file)
>
> C:\windows\system32>
>
>
> It's part of the stdio.h ... canon, which, SFAIK well
> antedates and has no dependencies on posix.

The fileno() function is not defined by the ISO C standard.  It is
defined by POSIX.  It's declared in <stdio.h> for historical reasons
that probably predate the C and POSIX standards, but the C standard
committee chose not to standardize it (or any functions that work with
POSIX-style file descriptors).

(IMHO it would have been better if all POSIX-specific declarations
were separate headers not defined by ISO C.)

A conforming ISO C implementation may not unconditionally define the
fileno() function in <stdio.h>, since a strictly conforming program is
free to use that identifier for its own purposes.  A conforming POSIX
implementation must do so.  Which means that, in order to use
POSIX-specific functions, you have to invoke the compiler in a
non-ISO-C-conforming mode (perhaps by defining a macro and/or using a
particular command-line option).

>> and that finding the file
>> size on Linux and Windows were two different procedures.
>
> This should also not be.

And yet it is.

[...]

> Your original plan should be just fine. This is the way I have 
> determined file sizes since the 20th Century, modulo declariong
> and initializing things at the same time for some cruder toolchains:
>
> FILE *f = fopen(....);
>
> int rc = fseek(f,SEEK_END,0L);
> int len = ftell(f);
> rc = fseek(f,SEEK_SET,0L);
>
> after which, the file is nicely rewound and len contains the total 
> number of octets in the file.
>
> Obviously  for some file systems this may not work, but those are
> hard to find now.

As we've discussed here recently, the C standard does not guarantee
that that will work.  The value returned by ftell() is not guaranteed
to be meaningful, and neither fseek() nor ftell() is guaranteed to
work at all on all files.  On Windows, the size of a file generally
does not match the number of characters that can be read from it
in text mode.

POSIX systems have the stat() and fstat() functions, which will give
you the size of a file, among other information (I think they'll
typically report 0 for special files that don't have a size).
Windows has its own function(s) for doing the same kind of thing.
(And yes, those functions will usually give you the same result as
the fseek/ftell method.)

Any method for determining the size of a file can only tell you what
the size was when you checked it.  A file can grow, shrink, become
inaccessible, or cease to exist between the size determination and
a subsequent attempt to read it.  Which doesn't mean that trying
determine its size is useless, but code to access the file should
be robust in the presence of possible changes.  It's sensible to
use the apparent size as an *estimate* of how much memory you'll
need to store its contents.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 05:11 +0200
  Re: Working with files on different operating systems Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-10-08 22:12 -0600
  Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 00:18 -0500
    Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 11:13 +0100
      Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 18:05 +0200
        Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 12:49 -0500
        Re: Working with files on different operating systems Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 17:32 -0400
        Re: Working with files on different operating systems Ian Collins <ian-news@hotmail.com> - 2015-10-10 12:37 +1300
      Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 12:48 -0500
        Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:53 +0100
          Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 21:09 +0200
            Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 20:28 +0100
              Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 21:40 +0200
                Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-09 15:59 -0400
                Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 22:07 +0200
                Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-09 16:24 -0400
                Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-10 03:08 +0200
                Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 21:50 +0100
        Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-09 14:54 -0400
          Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 21:05 +0200
            Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 21:47 +0200
            Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-09 15:53 -0400
              Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 22:01 +0200
                Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-09 16:05 -0400
          Re: Working with files on different operating systems glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-10 01:16 +0000
            Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-10 03:46 +0200
            Re: Working with files on different operating systems Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-10 08:08 -0400
          Re: Working with files on different operating systems raltbos@xs4all.nl (Richard Bos) - 2015-10-10 17:02 +0000
        Re: Working with files on different operating systems Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 15:53 -0400
    Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 18:00 +0200
      Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 12:59 -0500
        Re: Working with files on different operating systems Keith Thompson <kst-u@mib.org> - 2015-10-09 12:37 -0700
          Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 17:36 -0500
            Re: Working with files on different operating systems Keith Thompson <kst-u@mib.org> - 2015-10-09 16:52 -0700
              Re: Working with files on different operating systems raltbos@xs4all.nl (Richard Bos) - 2015-10-14 11:27 +0000
                Re: Working with files on different operating systems James Kuyper <jameskuyper@verizon.net> - 2015-10-14 11:27 -0400
                Re: Working with files on different operating systems Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-14 16:46 +0000
                Re: Working with files on different operating systems Keith Thompson <kst-u@mib.org> - 2015-10-14 10:18 -0700
              Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-14 23:06 -0500
    Re: Working with files on different operating systems Keith Thompson <kst-u@mib.org> - 2015-10-09 09:16 -0700
      Re: Working with files on different operating systems Les Cargill <lcargill99@comcast.com> - 2015-10-09 17:43 -0500
        Re: Working with files on different operating systems Keith Thompson <kst-u@mib.org> - 2015-10-09 16:57 -0700
          Re: Working with files on different operating systems Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-09 22:41 -0700
    Re: Working with files on different operating systems David Thompson <dave.thompson2@verizon.net> - 2015-10-31 06:50 -0400
  Re: Working with files on different operating systems Siri Cruz <chine.bleu@yahoo.com> - 2015-10-08 23:25 -0700
    Re: Working with files on different operating systems Geoff <geoff@invalid.invalid> - 2015-10-10 10:35 -0700
  Re: Working with files on different operating systems Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 08:42 -0400
    Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-09 18:51 +0200
      Re: Working with files on different operating systems Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-15 18:09 -0700
        Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-17 04:01 +0200
          Re: Working with files on different operating systems Ian Collins <ian-news@hotmail.com> - 2015-10-17 15:55 +1300
            Re: Working with files on different operating systems Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-16 20:09 -0700
              Re: Working with files on different operating systems Ian Collins <ian-news@hotmail.com> - 2015-10-17 16:36 +1300
            Re: Working with files on different operating systems "Morten W. Petersen" <morphex@gmail.com> - 2015-10-17 05:10 +0200
        Re: Working with files on different operating systems supercat@casperkitty.com - 2015-10-31 12:05 -0700
          Re: Working with files on different operating systems Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-31 12:42 -0700
            Re: Working with files on different operating systems supercat@casperkitty.com - 2015-10-31 13:40 -0700
          Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 19:55 +0000
            Re: Working with files on different operating systems supercat@casperkitty.com - 2015-10-31 13:43 -0700
              Re: Working with files on different operating systems Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 21:38 +0000

csiph-web