fopen/fclose 上的段错误

发布于 2024-11-15 17:03:28 字数 1494 浏览 4 评论 0原文

我有一个正在创建多个文件的程序。每个正在创建的文件都有一个函数。每个函数中都有完全相同的代码来创建文件名、打开/创建文件以进行写入、设置其权限以及最后关闭文件。我决定创建一个用于打开文件和关闭文件的函数,这样我就可以调用它,而不是每次都使用相同的代码。以前每个函数中的代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-11-22 17:03:28

这段代码是错误的:

void OpenFile(FILE *file, char *name) {
   if (!(file = fopen(name, "w"))) {

这里您只是分配给本地 file 变量。
您必须返回文件,以便您的 WriteFile1() 函数可以使用该 FILE*

void WriteFile1() {
FILE *file;

file = OpenFile("filename.asdf");
//fprintf statements
CloseFile(file, "filename.asdf");
}

FILE * OpenFile(char *name) {
   FILE * file;
   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);
   return file;
}

This piece of code is wrong:

void OpenFile(FILE *file, char *name) {
   if (!(file = fopen(name, "w"))) {

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*

void WriteFile1() {
FILE *file;

file = OpenFile("filename.asdf");
//fprintf statements
CloseFile(file, "filename.asdf");
}

FILE * OpenFile(char *name) {
   FILE * file;
   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);
   return file;
}
踏雪无痕 2024-11-22 17:03:28

您的 open 函数应如下所示:

FILE *  OpenFile( char *name) {
   FILE * file;
   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);
   return file;
}

在您的版本中, FILE * 实际上是函数的局部变量(因为它是参数)。在函数中更改它不会在外部世界中更改它。

在设计返回指针(或任何其他相关内容)的函数时,始终更喜欢通过 return 语句返回指针,而不是尝试通过参数列表来返回指针。

Your open function should look like this:

FILE *  OpenFile( char *name) {
   FILE * file;
   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);
   return file;
}

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.

楠木可依 2024-11-22 17:03:28

这:

filename = malloc(sizeof(char *) * (strlen(name) + 1))

应该是:

filename = strdup(name);

如果你有它,否则类似于:

if((filename = malloc(strlen(name) + 1)) != NULL)
{
   strcpy(filename, name);
   ...
}

特别注意,每个字符只是一个 char,而不是 char *。由于 sizeof (char) == 1 始终为 true,因此根本没有必要涉及它。

This:

filename = malloc(sizeof(char *) * (strlen(name) + 1))

should be:

filename = strdup(name);

if you have it, otherwise something like:

if((filename = malloc(strlen(name) + 1)) != NULL)
{
   strcpy(filename, name);
   ...
}

Note in particular that each character is just a char, not a char *. Since sizeof (char) == 1 is always true, there's no point in involving it at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文