Programming in standard c

R

Richard Heathfield

Bart C said:

The return value in *sizep is still a little worrying because according
to user923005 this information is useless, if it's assumed to bear any
relation to the size of the file just read, because that size could
change any time.

No, it's useful information because it represents the amount of data
actually read from the file into main storage. Presumably, one reads the
data from file for a reason - either to manipulate it or at least to
enquire it. Either way, one will need to know how much data there is to
manipulate (or enquire).
 
E

Eric Sosman

Richard said:
user923005 said:
[...]
a generic function cannot produce a reliable answer. I guess that
other people have thought about this question and figured out that a
reliable file size function cannot be written in a generic manner.

Rubbish. All files have a byte size.

How many bytes does /dev/tty hold? How about /dev/zero?
/dev/ptyp0? /dev/random? /dev/poll? CON:? LPT:?
How silly.

"correct" would be platform specific. The API need not be.

One could assign an arbitrary pseudo-size -- minus one,
perhaps -- to the troublesome files, and then declare that
this pseudo-size was the "correct by decree" size of the
file. However, the value would then be useless for the
purpose to which Jacob's "Happy christmas" code wanted: to
determine how many bytes to allocate to hold the file's
content.

And even for an ordinary file whose length can be known,
many systems permit the length to change between the moment
it's queried and the moment the result is used. How many bytes
should you allocate for an in-memory copy of /var/adm/messages?
 
J

jameskuyper

jacob said:
Randy Howard wrote: ....

Nobody needs to modify the OS. But if those systems support C, they
MUST support

FILE *f = fopen("foo","a+");

And they HAVE to know where the end of the file is somehow. I am
amazed how you and the others just ignore the basic facts.

The fact that a conforming implementation of C must support "a+" mode
does not imply that it must have an efficient way of determining how
many bytes separate the beginning of the file from its end..

7.19.3:
"If a file can support positioning requests (such as a disk file, as
opposed to a terminal), then a file position indicator associated with
the stream is positioned at the start (character number zero) of the
file, unless the file is
opened with append mode in which case it is implementation-defined
whether the file position indicator is initially positioned at the
beginning or the end of the file."

Note a couple of important points:
1. A file need not be able to support positioning requests.
2. The file position indicator does not necessarily point to the end
of the file, just because you opened it in "a+" mode. As has already
been discussed, an implementation need not support SEEK_END in any
meaningful way. Which means that, on a given platform, the only way to
reach the end of the file might to read through the entire file. A
conforming implementation of the C standard library might have to do
precisely that, in order to perform the first write to a file opened
in "a+" mode.
 
B

Bart C

Eric Sosman said:
Richard wrote:

How many bytes does /dev/tty hold? How about /dev/zero?
/dev/ptyp0? /dev/random? /dev/poll? CON:? LPT:? ....
And even for an ordinary file whose length can be known,
many systems permit the length to change between the moment
it's queried and the moment the result is used. How many bytes
should you allocate for an in-memory copy of /var/adm/messages?

These are all very interesting examples which should be kept in mind when
writing mission-critical code or code for life-support systems.

But there is a distinct class of well-behaved files (input files of a
compiler for example), which are unlikely to be huge and unlikely to change.
For a lot of applications and their files, this will be the case.

With shared files on multi-user/multi-process systems there can be pitfalls,
but size of the file suddenly changing would be the least of the problems.

I don't know how to deal with /var/adm/messages or similar. Suppose I read
byte-by-byte as recommended then someone updates the beginning of the file?
Maybe it's foolhardy to even attempt making a copy of such a file. I'm not
allowed to lock it because that's also frowned upon. What exactly can one do
with such a file?


Bart
 
R

Robert Latest

Bart said:
I don't know how to deal with /var/adm/messages or similar. Suppose I read
byte-by-byte as recommended then someone updates the beginning of the file?
Maybe it's foolhardy to even attempt making a copy of such a file. I'm not
allowed to lock it because that's also frowned upon. What exactly can one do
with such a file?

You need to KNOW what you can do with a file before starting to do stuff
with it. That's an excellent reason why a generic gobble-into-RAM or
filesize function just doesn't make any sense.

robert
 
B

Ben Bacarisse

/*
* Our estimate must have been too low. We actually
* have room to save c, so do that now, then enlarge
* the buffer and try again.
*/
buf[n] = c;
estimate *= 2; /* or any other suitable increase */
new = malloc(estimate + 1);

Presumably you meant realloc here?

Nice example of hiving off the minimum of system specific code, BTW.
 
E

Eric Sosman

Bart said:
[...]
I don't know how to deal with /var/adm/messages or similar. Suppose I read
byte-by-byte as recommended then someone updates the beginning of the file?
Maybe it's foolhardy to even attempt making a copy of such a file. I'm not
allowed to lock it because that's also frowned upon. What exactly can one do
with such a file?

There are at least two problems to be faced when dealing
with /var/adm/messages. First, the file grows as the system
appends new log messages to it, an activity not coordinated
with the actions of J. Random Program. If JRP queries the
file size, then allocates a buffer of that size, and then
tries to fill the buffer by reading blindly to EOF, the file
may grow larger between the query and the reading. Thus, the
queried size must be considered an estimate, not an absolute:
use Chris Torek's approach, not Jacob Navia's.

The second problem is that the system does not allow the
file to grow without limit. Every now and then, it renames
the existing /var/adm/messages and creates a new, empty one
where future messages are deposited. So the semantics of how
the system identifies "a" file become important: Are the query
and the reading directed to the same bunch of bytes, or only
to the same file name? This is the (or a) reason POSIX systems
have both the stat() and fstat() query operations: the first
asks about the file associated with a given name, while the
second asks about a file that's already open and whose name
is no longer relevant.

So how does one process /var/adm/messages, or more broadly,
how does one process a file that may be subject to change while
the processing is in progress? The C language has almost no
support for parallel activities; signals and volatile are about
the extent of it, and neither is useful without a slew of
platform-specific semantics that C itself does not legislate.
(The question of whether the committee chose well or poorly in
declining to so legislate is an issue for some other forum.)
So when you're processing a file that is being modified by other
agencies, you must begin by expanding your view to encompass not
only the language you want to use, but also the environment in
which you want to use it.

That wider programming environment can provide guarantees
of various kinds. One might be "Your program has exclusive
access to the file for the interval of interest," which seems
to be the unstated but crucial assumption in some of the arguments
elsethread. It's a guarantee that can often be provided -- by
voluntary convention, if nothing else -- but it's certainly not
provided for all files under all circumstances in all environments.
Other guarantees might be "This file is only appended to" or "It
is possible to acquire an exclusive lock on all or part of this
file" or "Your program can arrange to be notified whenever something
happens to this file." You make use of this additional platform-
specific and/or application-specific knowledge as best you can for
the task at hand.

Personally, I don't think much of the read-it-all-in approach
to handling data that's in files. That's because I'm from the
Old School, and learned my craft in the days when memory was
scarce and expensive. I'm always worried that I won't be able to
fit the file into the available memory, so I look for ways to
process the data one "window" at a time: Rather than read the whole
thing, process it, and write the results, I try to read-process-
write-lather-rinse-repeat, and thus not place such an obvious and
inflexible upper limit on the size of the file I can handle. It
doesn't always work, of course -- sometimes you really do need the
entire contents of the file -- but even then you seldom need to
have the whole thing sitting in one big contiguous array. You
read the enormous CAD model and build data structures while reading
it; all the data winds up in memory, but you never have or need
an "image" of the entire file as it looks on disk.
 
D

dj3vande

CBFalconer said:
Remember that ALL computers act just like Winders. Anything else
is not to be considered.

Interestingly, my current hobby project is sidetracked while I figure
out how to deal with multiple incompatible implementations[1] of the
Windows API.
So even when all the computers in your universe act just like Winderz,
a fanatical devotion to using portable constructs where you can and
cleanly separating the non-portable constructs that you find you must
use is still a net benefit.

ObTopic: Dealing with this has been made a lot easier by having written
big chunks of the code in portable standard C; when I find something in
the system interface that's broken I can ignore those parts entirely
and have much less code to worry about fixing.


dave

[1] "Native" Win32, Wine on Linux, and Wine on MacOS. I've run into at
least one not-all-that-weird construct that's broken on one of
those and works sensibly (but differently) on the other two.
 
U

user923005

I think we can improve this a great deal, with the result being
a function that is written entirely in Standard C and works in
every case in which it is possible for it to work, and -- by
calling a system-dependent function that the user is to supply,
but which may be replaced with a #define that simply returns 0
if desired -- is "reasonably efficient" as well.
[snip]

I work for a database company, and most of our customers are large
customers. (E.g. huge US company, large university, government of
country x, etc.)

It is not at all unusual for a single file to be 20-100 GB. Needless
to say, you would not want to put this file into memory even if you
could do it.
The files I work with are also being modified constantly (though there
are occasionally windows of inactivity for some of them).
I am quite sure that the goal of reliably reading these sorts of files
into memory and doing something useful with them is literally
infeasible (not impossible, but the cost would make it so stupid that
nobody would want to do it).

I think that usually, unless you lock the entire file, reading an
entire file into memory is begging for trouble. Perhaps the
probability of the file being extended while you have it open is less
than 0.1%. But would you rely on a strcpy() that was only 99.9%
reliable? Similarly, someone modifying the contents in the middle
while you have it in memory might have one chance in 10,000. But that
one instance can spell disaster. Now, some kinds of files beg to be
mapped into memory (e.g. executable files). So most operating systems
will have a provision for this. The memory mapping APIs will have
already worked some of the the issues out that arise when trying to
read files into memory (e.g. 'What if it won't fit?'). For many file
types it makes literally no sense to try to map them into memory.

I think it can be possible to do what ACE (by D. Schmidt) has done
portably across a huge number of systems (because *cough* they have
already done it). This includes highly portable memory mapping,
threads, semaphores, etc.. What they have accomplished could also be
accomplished in C (or even if it is too difficult, it could be done
via a shared library to that existing project). But memory mapping is
more than reading a file into memory, and it only makes sense for
files that we know are totally static or which we have total control
over (single user).

My opinion is that Jacob chose what is probably one of the most
difficult possible projects to ridicule what is possible in standard
C, and also that he probably knew it before hand. He acts like a
dummy, but he isn't one.
 
J

jacob navia

user923005 said:
I work for a database company, and most of our customers are large
customers. (E.g. huge US company, large university, government of
country x, etc.)

It is not at all unusual for a single file to be 20-100 GB. Needless
to say, you would not want to put this file into memory even if you
could do it.


Great!

So don't do it!

Nobody forces you to do that. The usage of all functions in standard
library is OPTIONAL, you are NOT forced to use any of them.

For instance for those files fseek or ftell will not work if a long
is just 32 bits.

Your requirements are unusual I would say. Many applications use
configuration files, or other data files of size of a few K or at
most 1MB. In those situations a portable function to read the
whole file into memory would be useful.
The files I work with are also being modified constantly (though there
are occasionally windows of inactivity for some of them).

In many applications, the data files can be considered static, i.e.
they do not change during the applications lifetime. This is specially
true of configuration files, C source files, object files, makefiles,
and all developer related files.
I am quite sure that the goal of reliably reading these sorts of files
into memory and doing something useful with them is literally
infeasible (not impossible, but the cost would make it so stupid that
nobody would want to do it).

For 20GB files this would be a bit difficult with today's memory sizes.
For 100GB files this makes no sense at all.
I think that usually, unless you lock the entire file, reading an
entire file into memory is begging for trouble.

It depends. Not everyone is working with the files you are working.
You say very often that "not all the world is a windows PC". Well,
not everybody is working with such huge files all the time isn't it?

And even YOUR application, I would bet that it has some configuration
files it reads at startup. Those aren't 100GB files but probably
20K-100K files.
Perhaps the
probability of the file being extended while you have it open is less
than 0.1%. But would you rely on a strcpy() that was only 99.9%
reliable? Similarly, someone modifying the contents in the middle
while you have it in memory might have one chance in 10,000. But that
one instance can spell disaster. Now, some kinds of files beg to be
mapped into memory (e.g. executable files). So most operating systems
will have a provision for this. The memory mapping APIs will have
already worked some of the the issues out that arise when trying to
read files into memory (e.g. 'What if it won't fit?'). For many file
types it makes literally no sense to try to map them into memory.

The problem with memory mapping is that it is very system specific.
A single STANDARD function that would do that in all supported
platforms would be quite useful. Normally you are not so interested
in performance for those relatively small files, but it would be nice
if you wouldn't have to write it again in each system you go to.

I think it can be possible to do what ACE (by D. Schmidt) has done
portably across a huge number of systems (because *cough* they have
already done it). This includes highly portable memory mapping,
threads, semaphores, etc.. What they have accomplished could also be
accomplished in C (or even if it is too difficult, it could be done
via a shared library to that existing project). But memory mapping is
more than reading a file into memory, and it only makes sense for
files that we know are totally static or which we have total control
over (single user).

My opinion is that Jacob chose what is probably one of the most
difficult possible projects to ridicule what is possible in standard
C, and also that he probably knew it before hand. He acts like a
dummy, but he isn't one.

No, I just wanted to make a point about the difficulty of using
standard C for a simple task like reading a file into memory.

And forward some solutions, like the standard error handling, or
a filesize() function.
 
J

jacob navia

Chris Torek wrote:

[snip]

This is possible in standard C.

You are forced to read character by character of data, until you reach
the end of the file. This is maybe ok (disk I/O could be the limiting
factor here) but ignores the problem I tried to solve concerning
abstracting the text/binary difference.

If we had a function called filsize() in the standard (one of my main
complaints) your program could be written in a few lines, and read
all the size of the file into memory.
 
S

Serve La

"user923005" <[email protected]> schreef in bericht
user923005 said:
Here is a machine (HP RX/1600 Itanium running OpenVMS) that reports
its file sizes in blocks.

[big snip]


Total of 28 files, 4404 blocks.
Next Cmd:
each of those file sizes is 512 byte increments. You can buy a new
one today from HP, if you want.
http://h71000.www7.hp.com/openvms/hw_supportchart.html

If you have one of that systems and your trash is full so you can't get
rid of it, then you can do the following

http://h71000.www7.hp.com/wizard/wiz_5424.html
Obtaining RMS file size?
Hello Mr Wizard

I am looking for a method of obtaining the size of a file, NOT the
allocation
from within a Fortran routine. I thought this would be a simple
trivial task
but alas I am proved wrong.

Can you help.

Regards Jim
Answer
RMS does NOT keep track of the number of user data bytes in a file.
The only reliable way to obtain that, is to read the file and count!

So there you have the answer!

If you are using that system expect that filesize() will take some time.

So What?

Should we ALL suffer because some brain-dead system exists somewhere?

remove() is in the standard while there are lots of systems without a
filesystem and where remove doesnt have any meaning. I happen to work on on
such a system now but I dont know what remove does because I just dont need
it and never checked. I'm guessing it returns -1
So why cant a function like filesize() be added? On systems where its
meaningless it could return an error and the programmer can take over with
the byte by byte read or whatever works for him when filesize returned an
error. Whats the logic for adding remove and not adding filesize?
 
B

Ben Pfaff

user923005 said:
I work for a database company, and most of our customers are large
customers. (E.g. huge US company, large university, government of
country x, etc.)

It is not at all unusual for a single file to be 20-100 GB. Needless
to say, you would not want to put this file into memory even if you
could do it.

Sometimes you do. For some time, I worked for a company that had
a gigantic Perforce repository[*]. Every developer made heavy
use of this repository. Unfortunately, Perforce doesn't scale to
anything that big or that busy. The solution turned out to be to
put 128 GB of RAM in the Perforce server. Then the whole
database was cached. Performance was then tolerable, if still
not all that great.

[*] Perforce is a version control system.
 
S

Serve La

Eric Sosman said:
Richard said:
user923005 said:
[...]
a generic function cannot produce a reliable answer. I guess that
other people have thought about this question and figured out that a
reliable file size function cannot be written in a generic manner.

Rubbish. All files have a byte size.

How many bytes does /dev/tty hold? How about /dev/zero?
/dev/ptyp0? /dev/random? /dev/poll? CON:? LPT:?

Programmers should be able to understand that doing filesize("LPT1:") or
something would be silly and an incorrect value would be returned or rather
an errorcode. That there are some files of which the size cant be determined
should not be a reason to not add such a function to the standard. I dont
expect remove() to work on *every* file that exists on a system
 
U

user923005

"user923005" <[email protected]> schreef in bericht
[big snip]
Total of 28 files, 4404 blocks.
Next Cmd:
each of those file sizes is 512 byte increments. You can buy a new
one today from HP, if you want.
http://h71000.www7.hp.com/openvms/hw_supportchart.html
If you have one of that systems and your trash is full so you can't get
rid of it, then you can do the following
http://h71000.www7.hp.com/wizard/wiz_5424.html
Obtaining RMS file size?
Hello Mr Wizard
I am looking for a method of obtaining the size of a file, NOT the
allocation
from within a Fortran routine. I thought this would be a simple
trivial task
but alas I am proved wrong.
Can you help.
Regards Jim
Answer
RMS does NOT keep track of the number of user data bytes in a file.
The only reliable way to obtain that, is to read the file and count!
So there you have the answer!
If you are using that system expect that filesize() will take some time.
Should we ALL suffer because some brain-dead system exists somewhere?

remove() is in the standard while there are lots of systems without a
filesystem and where remove doesnt have any meaning. I happen to work on on
such a system now but I dont know what remove does because I just dont need
it and never checked. I'm guessing it returns -1
So why cant a function like filesize() be added? On systems where its
meaningless it could return an error and the programmer can take over with
the byte by byte read or whatever works for him when filesize returned an
error. Whats the logic for adding remove and not adding filesize?

Because filesize can never be anything but an estimate {in the generic
case}. For special types of files, it has meaning. For other file
types it is begging for trouble.

Back to the original question (reading a file into memory), for
systems where this is sensible to do, there is going to exist a memory
map method. Memory mapping can be made relatively portable. That is
a far more sensible solution than simply trying to read a file into
memory (note: memory mapping does a lot more than just reading a file
into memory).

I think that adding a function to the standard library that produces
an unreliable answer has limited utility. The cases where we need the
absolute number will also require lots of guarantees about the file
properties. It is starting to smell operating system specific, n'est
ce pas? For example, someone might use that number to try to read the
file into memory. That is clearly a mistake. Instead, they should
memory map the file if their system supports it.

I guess that filesize() is not in the standard because the C language
implementors thought about it and realized that people might try to
use it. Then, they will have thousands of support calls to handle
when things go wrong.

On the other hand, I think that there was a call somewhere in this
thread for fileinformation() which I think might be a very nice
addition. I guess that even filesize() might not be so bad if they
renamed it currentfilesize(). Then (at least) it would be obvious
that it only contains an estimate.
 
B

Bart C

Eric Sosman said:
Bart said:
[...]
I don't know how to deal with /var/adm/messages or similar. Suppose I
read byte-by-byte as recommended then someone updates the beginning of
the file? Maybe it's foolhardy to even attempt making a copy of such a
file. I'm not allowed to lock it because that's also frowned upon. What
exactly can one do with such a file?

There are at least two problems to be faced when dealing
with /var/adm/messages. First, the file grows as the system

Well, we can class that sort of file as Special and deal with it With Care.
Or pehaps choose not to deal with it at all. The point is most input files
(it is mostly input ones you want to read!) are known to be well-behaved (eg
config files/support files of an application) and can be dealt with less
strictly.

If we have to worry about multi-process access than we may have to consider
our entire application may suddenly disappear in a puff of smoke. It's best
not to worry about it.
Personally, I don't think much of the read-it-all-in approach
to handling data that's in files. That's because I'm from the
Old School, and learned my craft in the days when memory was
scarce and expensive.

Same here.
You
read the enormous CAD model and build data structures while reading
it; all the data winds up in memory, but you never have or need
an "image" of the entire file as it looks on disk.

Yes, I've written CAD-type software for 8-bit and MSDOS. That was fun.

Now, however, you've got 1GB RAM or whatever sitting there and you need to
do *something* with it. I'm talking about at least desktop PCs of course.

Bart
 
F

Flash Gordon

jacob navia wrote, On 28/12/07 20:50:
Great!

So don't do it!

Nobody forces you to do that. The usage of all functions in standard
library is OPTIONAL, you are NOT forced to use any of them.
True.

For instance for those files fseek or ftell will not work if a long
is just 32 bits.

This is why C provides other functions. It is also why systems that can
do it often provide other methods.
Your requirements are unusual I would say. Many applications use
configuration files, or other data files of size of a few K or at
most 1MB. In those situations a portable function to read the
whole file into memory would be useful.

I can't say I've needed (or wanted) to do it even with small files.
In many applications, the data files can be considered static, i.e.
they do not change during the applications lifetime. This is specially
true of configuration files, C source files, object files, makefiles,
and all developer related files.

I would not go as far as all developer related files, but some files
yes. However I would actually do the first pass on files in to an
internal format whilst reading them normally rather than reading them
and then passing them.
For 20GB files this would be a bit difficult with today's memory sizes.
For 100GB files this makes no sense at all.


It depends. Not everyone is working with the files you are working.
You say very often that "not all the world is a windows PC". Well,
not everybody is working with such huge files all the time isn't it?

And even YOUR application, I would bet that it has some configuration
files it reads at startup. Those aren't 100GB files but probably
20K-100K files.

I've never needed to read an entire configuration file in to memory at
one time. I pass it as I read it since what I need in memory is *not*
the file but the information it provides.

No, I just wanted to make a point about the difficulty of using
standard C for a simple task like reading a file into memory.

It is only difficult to do it with the method you used. Doing it growing
a buffer as required is easy. If you want to do something easily in
standard C you have to actually try to do it in the ways standard C allows.
And forward some solutions, like the standard error handling, or
a filesize() function.

Some of which may actually be nice, but since you put forward your
suggestions by attacking everyone and standard C you should not be
surprised by lack of sympathy for your position. Also claiming that any
systems where it is a problem to do what you want do not matter does not
help your case.
 
U

user923005

Bart said:
[...]
I don't know how to deal with /var/adm/messages or similar. Suppose I
read byte-by-byte as recommended then someone updates the beginning of
the file? Maybe it's foolhardy to even attempt making a copy of such a
file. I'm not allowed to lock it because that's also frowned upon. What
exactly can one do with such a file?
    There are at least two problems to be faced when dealing
with /var/adm/messages.  First, the file grows as the system

Well, we can class that sort of file as Special and deal with it With Care..
Or pehaps choose not to deal with it at all. The point is most input files
(it is mostly input ones you want to read!) are known to be well-behaved (eg
config files/support files of an application) and can be dealt with less
strictly.

If we have to worry about multi-process access than we may have to consider
our entire application may suddenly disappear in a puff of smoke. It's best
not to worry about it.
    Personally, I don't think much of the read-it-all-in approach
to handling data that's in files.  That's because I'm from the
Old School, and learned my craft in the days when memory was
scarce and expensive.

Same here.
You
read the enormous CAD model and build data structures while reading
it; all the data winds up in memory, but you never have or need
an "image" of the entire file as it looks on disk.

Yes, I've written CAD-type software for 8-bit and MSDOS. That was fun.

Now, however, you've got 1GB RAM or whatever sitting there and you need to
do *something* with it. I'm talking about at least desktop PCs of course.

Contrast the:
1. Find file size
2. Allocate that size
3. Read file into RAM
4. Modify file
5. Write file to disk

Approach to:
1 Use memory mapping.

Seem identical? Now imagine if the file is on a shared drive or if
the file won't fit into physical RAM (or would consume more RAM than
desired).
 
C

CBFalconer

Chris said:
.... snip ...

/* Loop ended, and not due to error, so must be normal EOF. */
#ifdef OPTIONAL
if (n < space) {
new = realloc(buf, n + 1);
if (new != NULL)
buf = new;
}
#endif
buf[n] = '\0';
if (sizep != NULL)
*sizep = n;
return buf;
}

Minor error. If the realloc fails, buf[n] = '\0' will write beyond
the buffer. Try:

if (n >= space) buf[n] = '\0';
else if (new = realloc(buf, n + 1)) {
buf = new; buf[n] = '\0';
}
else buf[n - 1] = '\0'; /* minor loss */

if (sizep) *sizep = n;
return buf;
}

I haven't bothered to examine the meaning of OPTIONAL, which may
make a difference. However this code makes me suspicious of the
relationship between n and space.
 
F

Flash Gordon

Serve La wrote, On 28/12/07 21:01:
"user923005" <[email protected]> schreef in bericht
user923005 said:
Here is a machine (HP RX/1600 Itanium running OpenVMS) that reports
its file sizes in blocks.

[big snip]


Total of 28 files, 4404 blocks.
Next Cmd:
each of those file sizes is 512 byte increments. You can buy a new
one today from HP, if you want.
http://h71000.www7.hp.com/openvms/hw_supportchart.html

If you have one of that systems and your trash is full so you can't get
rid of it, then you can do the following

http://h71000.www7.hp.com/wizard/wiz_5424.html
Obtaining RMS file size?
Hello Mr Wizard

I am looking for a method of obtaining the size of a file, NOT the
allocation
from within a Fortran routine. I thought this would be a simple
trivial task
but alas I am proved wrong.

Can you help.

Regards Jim
Answer
RMS does NOT keep track of the number of user data bytes in a file.
The only reliable way to obtain that, is to read the file and count!

So there you have the answer!

If you are using that system expect that filesize() will take some time.

So What?

Should we ALL suffer because some brain-dead system exists somewhere?

remove() is in the standard while there are lots of systems without a
filesystem and where remove doesnt have any meaning. I happen to work on
on such a system now but I dont know what remove does because I just
dont need it and never checked.

All such systems I've come across are freestanding implementations, a
class which is not required to provide most of the standard C library.
If there is a hosted implementation without anything like a file system
and where remove() makes no sense I would be surprised.
I'm guessing it returns -1
So why cant a function like filesize() be added? On systems where its
meaningless it could return an error and the programmer can take over
with the byte by byte read or whatever works for him when filesize
returned an error. Whats the logic for adding remove and not adding
filesize?

One reason is probably existing practice when C was standardised.
Another is that if there is a file system remove() is *probably* easy to
implement, but filesize() is *definitely* problematic on some systems,
even to the point of defining what you mean by filesize being an
interesting problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top