fopen返回NULL - 哪里是导致错误的列表? [英] NULL returned by fopen - where is a list of what's causing the error?

查看:80
本文介绍了fopen返回NULL - 哪里是导致错误的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开附加文件。即。如果文件存在,打开

它是否可以附加,如果没有,则创建它。我回来了一个NULL指针,

,不知道为什么。这是我的代码:


FILE * pFile;

char * cFileName;

cFileName = argv [++ i];

pFile = fopen(cFileName," a +");


cFileName有一个有效的文件名(''apm.dbg''),并且我已经在目录中完整地写了
权限。我可以进入fopen,当打开文件

时,我可以在另一个窗口中看到该文件是使用

零字节创建的。然而,pFile返回为NULL。


是否有一个函数调用我可以告诉它实际上是什么错误?

解决方案

ferrad写道:

我正在尝试打开一个文件进行追加。即。如果文件存在,打开
它是否附加,如果没有,则创建它。我正在找回一个NULL指针,
并且不知道为什么。这是我的代码:

FILE * pFile;
char * cFileName;
cFileName = argv [++ i];
pFile = fopen(cFileName,"一个+ QUOT);


您的代码不可编辑/可运行。例如。什么是''我'?

cFileName有一个有效的文件名(''apm.dbg''),我在目录中有完整的写权限。我可以进入fopen,当打开文件
时,我可以在另一个窗口中看到该文件是用零字节创建的。但是pFile返回为NULL。

是否有函数调用我可以告诉它实际上是什么错误?




AFAIK, standard仅指定fopen()在因任何原因失败时返回NULL




7.19.5.3.8

fopen函数返回一个指向控制流的对象的指针。如果打开操作失败,fopen返回一个空的

指针。


干杯


弗拉基米尔

-

我的电子邮件地址是真的,我看了。




" ferrad" <交流*** @ hotmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

我正在尝试打开一个文件进行追加。即。如果文件存在,打开
它是否附加,如果没有,则创建它。我正在找回一个NULL指针,
并且不知道为什么。这是我的代码:

FILE * pFile;
char * cFileName;
cFileName = argv [++ i];
pFile = fopen(cFileName," a +");

cFileName有一个有效的文件名(''apm.dbg''),我在目录中有完整的写权限。我可以进入fopen,当打开文件
时,我可以在另一个窗口中看到该文件是用零字节创建的。但是pFile返回为NULL。

是否有函数调用我可以告诉它实际上是什么错误?




唯一的事情标准C表示返回值

''fopen()''如果

文件的打开失败则为NULL,如果失败则为非NULL它成功了。


失败的任何特殊原因都将取决于平台。

您应该查阅您的编译器和/或平台文档

(例如,对于错误代码列表,它会提供更多

特定信息)。许多实现为此目的提供了扩展



-Mike


ferrad写道:

我正在尝试打开一个文件进行追加。即。如果文件存在,打开
它是否附加,如果没有,则创建它。我正在找回一个NULL指针,
并且不知道为什么。这是我的代码:

FILE * pFile;
char * cFileName;
cFileName = argv [++ i];
pFile = fopen(cFileName," a +");

cFileName有一个有效的文件名(''apm.dbg''),我在目录中有完整的写权限。我可以进入fopen,当打开文件
时,我可以在另一个窗口中看到该文件是用零字节创建的。但是pFile返回为NULL。

是否有一个函数调用我可以告诉它实际上是什么错误?



如果你包含string.h和errno.h你可以使用strerror(errno)获取

a字符串来解释错误。


-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......它仍然有效......)


I am trying to open a file for appending. ie. if the file exists, open
it for appending, if not, create it. I am getting back a NULL pointer,
and do not know why. Here is my code:

FILE *pFile;
char *cFileName;
cFileName=argv[++i];
pFile = fopen(cFileName,"a+");

cFileName has a valid filename (''apm.dbg''), and I have full write
permissions in the directory. I can step into fopen and when the file
is opened, I can see in another window that the file is created with
zero bytes. However pFile is returned as NULL.

Is there a function call I can make to tell what the error actually is?

解决方案

ferrad wrote:

I am trying to open a file for appending. ie. if the file exists, open
it for appending, if not, create it. I am getting back a NULL pointer,
and do not know why. Here is my code:

FILE *pFile;
char *cFileName;
cFileName=argv[++i];
pFile = fopen(cFileName,"a+");
Your code is not compilable/runnable. E.g. what is ''i''?

cFileName has a valid filename (''apm.dbg''), and I have full write
permissions in the directory. I can step into fopen and when the file
is opened, I can see in another window that the file is created with
zero bytes. However pFile is returned as NULL.

Is there a function call I can make to tell what the error actually is?



AFAIK, the standard only specifies that fopen() returns NULL
when it fails for whatever reason:

7.19.5.3.8
The fopen function returns a pointer to the object controlling
the stream. If the open operation fails, fopen returns a null
pointer.

Cheers

Vladimir
--
My e-mail address is real, and I read it.



"ferrad" <ac***@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

I am trying to open a file for appending. ie. if the file exists, open
it for appending, if not, create it. I am getting back a NULL pointer,
and do not know why. Here is my code:

FILE *pFile;
char *cFileName;
cFileName=argv[++i];
pFile = fopen(cFileName,"a+");

cFileName has a valid filename (''apm.dbg''), and I have full write
permissions in the directory. I can step into fopen and when the file
is opened, I can see in another window that the file is created with
zero bytes. However pFile is returned as NULL.

Is there a function call I can make to tell what the error actually is?



The only thing standard C says about the return value of
''fopen()'' is that it will be NULL if the opening of the
file failed, and non-NULL if it succeeded.

Any particular reasons for failure will be platform-dependent.
You should consult your compiler and/or platform documentation
(e.g. for a list of ''error codes'' which would give more
specific information). Many implementations provide extensions
for this very purpose.
-Mike


ferrad wrote:

I am trying to open a file for appending. ie. if the file exists, open
it for appending, if not, create it. I am getting back a NULL pointer,
and do not know why. Here is my code:

FILE *pFile;
char *cFileName;
cFileName=argv[++i];
pFile = fopen(cFileName,"a+");

cFileName has a valid filename (''apm.dbg''), and I have full write
permissions in the directory. I can step into fopen and when the file
is opened, I can see in another window that the file is created with
zero bytes. However pFile is returned as NULL.

Is there a function call I can make to tell what the error actually is?


If you include string.h and errno.h you could use strerror(errno) to obtain
a string explaining the error.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


这篇关于fopen返回NULL - 哪里是导致错误的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆