fopen/fclose 上的段错误
我有一个正在创建多个文件的程序。每个正在创建的文件都有一个函数。每个函数中都有完全相同的代码来创建文件名、打开/创建文件以进行写入、设置其权限以及最后关闭文件。我决定创建一个用于打开文件和关闭文件的函数,这样我就可以调用它,而不是每次都使用相同的代码。以前每个函数中的代码如下所示:
void WriteFile1(char *name) {
FILE *file;
char *filename; //This is being malloc'ed because it initially consisted of multiple strings
if (!(filename = malloc(sizeof(char *) * (strlen(name) + 1)))) MallocError();
if (!(file = fopen(filename, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting \n", filename);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
//a bunch of fprintf statements here
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", filename);
exit(1);
}
}
这工作得很好。我没有任何问题。现在它看起来如下所示:
void WriteFile1() {
FILE *file;
OpenFile(file, "filename.asdf");
//fprintf statements
CloseFile(file, "filename.asdf");
}
void OpenFile(FILE *file, char *name) {
if (!(file = fopen(name, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting... \n", name);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
}
void CloseFile(FILE *file, char *name) {
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", name);
exit(1);
}
}
当我到达 WriteFile1() 中的第一个 fprintf 语句时,它就会出现段错误。我是否对 FILE 变量做了错误的操作?看起来它应该像以前一样工作。唯一的区别是文件名字符串的 malloc,我将其作为名称传递并在引号中给出实际值。
谢谢
I have a program that is creating multiple files. There is a function for each file being created. Within each function is the exact same code to create the file name, open/create the file for writing, set its permissions and at the end close the file. I decided to make a function for opening the file and closing the file so I can just call that instead of using the same code each time. The code previously looked like the following in each function:
void WriteFile1(char *name) {
FILE *file;
char *filename; //This is being malloc'ed because it initially consisted of multiple strings
if (!(filename = malloc(sizeof(char *) * (strlen(name) + 1)))) MallocError();
if (!(file = fopen(filename, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting \n", filename);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
//a bunch of fprintf statements here
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", filename);
exit(1);
}
}
This worked perfectly fine. I had no issues. Now it looks like the following:
void WriteFile1() {
FILE *file;
OpenFile(file, "filename.asdf");
//fprintf statements
CloseFile(file, "filename.asdf");
}
void OpenFile(FILE *file, char *name) {
if (!(file = fopen(name, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting... \n", name);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
}
void CloseFile(FILE *file, char *name) {
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", name);
exit(1);
}
}
It seg faults as soon as I get to the first fprintf statement in WriteFile1(). Am I doing something incorrectly with the FILE variable? It seems like it should work just like it did before. The only difference is the malloc of the filename string, which I am instead passing as name and giving the actual value in quotes.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码是错误的:
这里您只是分配给本地
file
变量。您必须返回
文件
,以便您的 WriteFile1() 函数可以使用该 FILE*This piece of code is wrong:
Here you are just assigning to the local
file
variable.You have to return the
file
, so your WriteFile1() function can work with that FILE*您的 open 函数应如下所示:
在您的版本中, FILE * 实际上是函数的局部变量(因为它是参数)。在函数中更改它不会在外部世界中更改它。
在设计返回指针(或任何其他相关内容)的函数时,始终更喜欢通过 return 语句返回指针,而不是尝试通过参数列表来返回指针。
Your open function should look like this:
In your version, the FILE * is effectively a local variable (as it is a parameter) of the function. Changing it in the function does not change it in the outside world.
When designing functions that return pointers (or anything else for that matter), always prefer to return the pointer via a
return
statement rather than trying to do it via the parameter list.这:
应该是:
如果你有它,否则类似于:
特别注意,每个字符只是一个
char
,而不是char *
。由于sizeof (char) == 1
始终为 true,因此根本没有必要涉及它。This:
should be:
if you have it, otherwise something like:
Note in particular that each character is just a
char
, not achar *
. Sincesizeof (char) == 1
is always true, there's no point in involving it at all.