在文件打开错误处理 [英] Error handling in file opening
问题描述
【问题1】
当我打开一个文件到一个函数,一般我做这样的事情:
INT READ_FILE(字符*文件名)
{
FILE *鳍; 如果(!(FIN = FOPEN(文件名,R)))
返回1; / * ... * / 返回FCLOSE(翅);
}诠释的main()
{
字符文件名[100]; 如果(READ_FILE(文件名))
{
PERROR(文件名);
出口(1);
} 返回0;
}
一般 0
返回值是错误的(是吗?),那么我可以改变previous code到:
INT READ_FILE(字符*文件名)
{
FILE *鳍; 如果(!(FIN = FOPEN(文件名,R)))
返回0; / * ... * / 返回FCLOSE(FIN)!;
}诠释的main()
{
字符文件名[100]; 如果(!READ_FILE(文件名))
{
PERROR(文件名);
出口(1);
} 返回0;
}
但我认为,第一code更干净。
另一种选择是唯一的变化返回1;
到返回-1;
(第一code,我写了)
什么是最好的版本?
【问题2】
如果我必须处理更多的错误,这是纠正一个code这样?
INT READ_FILE(字符*文件名,INT **兽医)
{
FILE *鳍; 如果(!(FIN = FOPEN(文件名,R)))
{
PERROR(文件名);
返回1;
} *兽医=的malloc(10 * sizeof的(INT));
如果(*兽医== NULL)
{
PERROR(内存分配错误。\\ n);
返回1;
} / * ... * / 返回FCLOSE(翅);
}诠释的main()
{
字符文件名[100];
INT *兽医; 如果(READ_FILE(文件名,和放大器;兽医))
出口(1); 返回0;
}
重新Q1:
一)大多数POSIX函数实际上返回-1(或小于0)的错误,不是0看(例如)的open()
,的close()
,阅读()
,的write()
等等。唯一的例外是POSIX调用返回指针,例如 fopen()函数
,它返回一个 FILE *
。这些返回 NULL
上的错误。
二)我code $我的C $ C到工作像POSIX的功能,这是类似许多Linux程序的内部结构。我称之为'的UNIX C标准。然而,许多C ++程序和Java程序中使用真正
成功和假
失败。当这些程序员转向C,他们用1成功,0失败。这是没有错的,但确实引起混乱(好,使我困惑)。最坏的结果是,当这两个标准都在相同的程序中使用。采摘标准,并坚持它比它的标准选择更重要。
三)我自己的选择(与Q1),将返回 1
上的错误(即按你的另一种选择线)。
重新Q2:基本上是正确的,是
a)如果你的程序是成功的,最好退出(0)
比返回0
我相信。
B)相当,你 PERROR
是由你。也许你要打印的误差的main()
。
c)用 PERROR
紧接着退出(1)
(或者一个不同的退出code根据错误)是合理的正常的,如果你没有清理做或清除在 atexit对
。
D)如果你正在返回的 FCLOSE()
上的错误,则返回,如果的fopen
失败的结果应 1
(或 EOF
)不为1,如果 FCLOSE()
失败则返回 EOF
(也称为 1
)。
E)尼特:您主
函数应具有参数(如 INT主(字符** argv的,INT ARGC)
)
[Question 1]
When I open a file into a function, generally I do something like this:
int read_file (char *filename)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
return 1;
/* ... */
return fclose(fin);
}
int main ()
{
char filename[100];
if ( read_file(filename) )
{
perror(filename);
exit(1);
}
return 0;
}
Generally 0
return value is for errors (right?) then I can change the previous code into:
int read_file (char *filename)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
return 0;
/* ... */
return !fclose(fin);
}
int main ()
{
char filename[100];
if ( !read_file(filename) )
{
perror(filename);
exit(1);
}
return 0;
}
But I think that the first code is more clean.
Another option is only change return 1;
into return -1;
(in the first code that I wrote).
What's the best version?
[Question 2]
If I must handle more errors, is it correct a code like this?
int read_file (char *filename, int **vet)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
{
perror(filename);
return 1;
}
* vet = malloc (10 * sizeof(int));
if ( *vet == NULL )
{
perror("Memory allocation error.\n");
return 1;
}
/* ... */
return fclose(fin);
}
int main ()
{
char filename[100];
int *vet;
if ( read_file(filename, &vet) )
exit(1);
return 0;
}
Re Q1:
a) Most POSIX functions actually return -1 (or <0) for errors, not 0. Look at (for instance) open()
, close()
, read()
, write()
and so forth. The exception is the POSIX calls that return pointers, e.g. fopen()
, which returns a FILE *
. These return NULL
on error.
b) I code my code to work like POSIX functions, which is similar the innards of many linux programs. I would call this 'the UNIX C standard'. However, many C++ programs and Java programs use true
for success and false
for failure. When these programmers move to C, they use 1 for success, and 0 for failure. This isn't wrong, but does cause confusion (well, causes me confusion). The worst result is when both standards are used in the same program. Picking a standard and sticking to it is more important than which standard you choose.
c) My own choice (in relation to Q1), would be to return -1
on error (i.e. as per your 'another choice' line).
Re Q2: mostly right, yes.
a) If your program is successful, better to exit(0)
than return 0
I believe.
b) Quite where you perror
is up to you. Perhaps you want to print the error in main()
.
c) Using perror
immediately followed by exit(1)
(or perhaps a different exit code depending on the error) is reasonable normal if you have no clean up to do or clean up within atexit
.
d) If you are returning the result of fclose()
on error, then the return if fopen
fails should be -1
(or EOF
) not 1 as if fclose()
fails it returns EOF
(otherwise known as -1
).
e) Nit: your main
function should have parameters (e.g. int main(char **argv, int argc)
)
这篇关于在文件打开错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!