jacob navia escreveu:
Cesar said:
jacob navia escreveu:
CBFalconer wrote:
If you just want the name under which the file was opened, save
that at fopen time. No load on the library, no muss, no fuss. See
the strcpy function.
Yes sure. And how do the other functions that need this information
will be able to get it????
Suppose
int readfromLog(FILE *fp)
{
char buf[512];
size_t bufsiz=sizeof(buf);
int s = fread(buf,1,bufsiz,fp);
if (s <= 0) {
// How do I get the name of the file here Mr Falconer?
}
}
You see the problem?
Also to comment Chris Dollin's remark. The idiomatic way of doing this
is not to write the name of the file in the body of 'if' but instead
construct the readfromLog() interface in a way it returns error codes
which are processed by the caller.
Then the same problem will appear in the caller. You will have to keep
that information in a associative list (or in a structure containing the
file) , then modify a lot of interfaces to pass an extra parameter or
a different structure than FILE *.
It is up to the user of the functionality you're creating to do what it
finds appropriate.
It would be a diversion from the main topic to be finicky on the example
you gave, but just to avoid abstractions: in the case of hypothetical
readFromLog(), how many logs would be in an application? Isn't easier to
the programmer call the function with a wrapper readUsersLog,
readMaterialsLog, etc., and then just give a hard coded (in the wrapper)
"Error reading users log.", etc.?
This is not possible when you are writing a library (and can't ask the
user to pass you the file name because the user may not know it, or
did not store it, etc)
My central comment and critic here is that your design of the library is
not correct.
In some place was this file opened and the name known, the common way of
doing in C is to get back to caller.
This is a discussion where the principle of keeping related information
in one place holds. The best place to keep the file name information is
in the fopen function since it is that function the constructor of
the FILE structure. Any other solution means a replication of effort
that should be done when constructing the object.
A FILE structure can be obtained without a name. . .
Also, the function [_]fname as proposed would have serious problems if
you attempt to use it in a POSIX environment where FILE *fmemopen() or
FILE *open_memstream() are expected to exist and they have no name
assosiated with.
How many times must I repeat the same thing? If there is no file name
you can always return NULL (meaning there is no file name) or some
made up name like "/\/\stdin/\/\"...
So how the user will know *what* NULL object is being the culprit? In
the large what would be the advantage?
If your library requires a set of functions you feel to report file
names, create a whole ensemble with the correct ones from start, like
(say) MYFILE *openLog(), int readFromLog(), int closeLog(), etc.
Better, less surprises for users that want a conforming program to run
compiled by your tools, and you have the API you're developing.