为什么“fopen”会这样?返回 NULL 指针?
我正在用 C 编程语言开发一个简单的文件拆分/合并程序。问题是,由于某种原因 fopen 返回 NULL,因此,我的程序在 fwrite 语句处崩溃。我该如何解决这个问题?
这是 C 文件:
int SplitFile(char* filename, char* output, size_t size)
{
char current_file_name[256];
int file_count = 0, i = 0;
FILE *file = fopen( filename, "rb" );
printf("split %s into chunks of %d named\n", filename, size);
if (!file)
return E_BAD_SOURCE;
else
{
output = (char *) malloc(size * sizeof(char));
if (output == NULL)
return E_NO_MEMORY;
else
{
int bytes_read = 0;
FILE *outFile;
do
{
bytes_read = fread(output, sizeof(char), size, file );
sprintf(current_file_name, "%s%04lu\n", "part", file_count++);
outFile = fopen (current_file_name, "wb" ); // THIS RETURNS NULL
fwrite(output, sizeof(char), bytes_read, outFile); //CRASHES ON THIS LINE
}
while ( bytes_read > 0 )
;
//fclose(outFile);
}
}
fclose(file);
printf("...\n");
return 0;
}
I'm working on a simple file splitter/merger program in the C programming language. The problem is, for some reason fopen returns NULL, and because of that, my program is crashing at the fwrite statement. How do I fix this?
Here is the C file:
int SplitFile(char* filename, char* output, size_t size)
{
char current_file_name[256];
int file_count = 0, i = 0;
FILE *file = fopen( filename, "rb" );
printf("split %s into chunks of %d named\n", filename, size);
if (!file)
return E_BAD_SOURCE;
else
{
output = (char *) malloc(size * sizeof(char));
if (output == NULL)
return E_NO_MEMORY;
else
{
int bytes_read = 0;
FILE *outFile;
do
{
bytes_read = fread(output, sizeof(char), size, file );
sprintf(current_file_name, "%s%04lu\n", "part", file_count++);
outFile = fopen (current_file_name, "wb" ); // THIS RETURNS NULL
fwrite(output, sizeof(char), bytes_read, outFile); //CRASHES ON THIS LINE
}
while ( bytes_read > 0 )
;
//fclose(outFile);
}
}
fclose(file);
printf("...\n");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正确的做法是检查
errno
当fopen
返回NULL
时。我猜你的问题是你试图写入一个文件系统,该文件系统不允许在文件名中使用
\n
,但这也可能是权限问题。The proper thing to do is check
errno
whenfopen
returnsNULL
.I'm going to guess that your problem is that you're trying to write to a filesystem that doesn't allow
\n
in filenames, but it could be a permissions issue as well.fopen
可能返回NULL
的原因有很多,包括(但当然不限于):找出责任的方法是深入研究
errno
代码。然而,仅仅因为您解决了这个特定错误并不意味着您可以假设
fopen
永远不会返回NULL
。当处理 I/O 操作时,您的代码只需要预料到失败。无法预测 I/O 操作是否成功,而且它们总是可能会失败。There are many reasons
fopen
can returnNULL
including (but certainly not limited to):The way to find out which is responsible is to dig into the
errno
code.However just because you resolve this particular error doesn't mean you can assume
fopen
will never returnNULL
. When dealing with I/O operations your code simply has to expect failure. It's not possible to predict the success of I/O operations, and they can always fail.这意味着该文件可能不存在,或者在访问文件时发生某些权限错误,例如“只读”或“写保护”,因此在这些情况下 fopen 将返回 0 (NULL 指针)。成功时它将返回一个文件指针作为处理程序。
fp=fopen("c:\\ABC.txt", "r");
不能与fp=fopen("c:\\abc.txt", "r ”);
。在 Linux 环境中使用
//
而不是\\
。PS:在 Linux 和类 Unix 操作系统中,文件名区分大小写。
It means that the file might not exist or some permission error occurred while accessing a file such as "Read-Only" or "Write-Protected", so in those cases fopen will return 0 (a NULL pointer). On success it will return a file pointer as a handler.
fp=fopen("c:\\ABC.txt", "r");
cannot be the same asfp=fopen("c:\\abc.txt", "r");
.Use
//
instead of\\
in a Linux environment.P.S.: In Linux and Unix-like operating systems file names are case-sensitive.
fopen for write 在第一次运行时返回 NULL 吗?
我注意到,在您保持打开文件进行写入但不关闭它们的同时。
尝试在 fwrite 之后添加 fclose(outFile):
您打开的文件数量可能超出操作系统允许的数量。
Is fopen for write return NULL in the first run?
I noticed that in the while you keep open files for write but not closing them.
Try to add fclose(outFile) after fwrite:
It is possible you open more files than your OS allows.
在 Unix 中,对于 fopen(),没有理由在传递给 fopen() 的文件名前面加上 ./。
In Unix, for fopen(), there is no reason to prepend ./ to a filename passed to fopen().
就我而言,我在一个 while 循环中再次读取同一个文件,但忘记关闭它。
我使用一个函数来读取文件并查找匹配项,该函数有一个
return;
语句,该语句在执行fclose(fp)
之前终止该函数:DIn my case, i was reading the same file all over again in a while loop and forgot to close it.
I used a function for reading the file and finding a match and the function had a
return;
statement that terminated the function before doingfclose(fp)
:D从可执行文件所在的位置检查为文件指定的路径。
就我而言,当两者都出现在同一位置时,我正在打开 c 文件中的文本文件。
一直提示找不到文件的错误。
将文件放入可执行文件的文件夹中,它开始工作。
The path given for the file is checked from wherever the executable is present.
In my case I was opening the text file in c file when both were present at the same place.
It was continuously giving the error of file not found.
Placed the file in the folder of executable and it started working.
就我而言,这是因为我试图在不存在的目录中创建文件。
In my case, it was because I was trying to create the file in a directory that does NOT exist.