微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么使用 fopen 会出现分段错误?

如何解决为什么使用 fopen 会出现分段错误?

我正在用 C 语言编写一个使用 fifos 的客户端-服务器模型。我发送一个文件名和一个唯一的fifo名称,以便客户端接收来自客户端的数据,服务器打开文件并将其第一行写入fifo。问题是,即使文件存在,我在打开它时也会出现分段错误。似乎 fopen() 函数有效,但我仍然收到错误消息。如果文件不存在,它只会发送一个空字符串。

这是client.c

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#define BUFSIZE 512

struct sent {
   char name[BUFSIZE];
   char fifo[BUFSIZE];
};


int main()
{
   char name[BUFSIZE];
   char recieved[BUFSIZE];

   int client_server_fifo;
   char cs_fifo[BUFSIZE] = "cs_fifo";

   int server_client_fifo;
   char sc_fifo[BUFSIZE];
   sprintf(sc_fifo,"sc_fifo_%d",getpid());

   struct sent *sent;

   mkfifo(sc_fifo,0777); 

   while(1) {
      printf("Write the name of the file: ");
      scanf("%s",name);

      printf("1111\n");
      client_server_fifo = open(cs_fifo,O_WRONLY);
      printf("2222\n");
      
      printf("%s",name);
      printf("%s",cs_fifo);

      sent->name = name;
      sent->fifo = cs_fifo;

      printf("%s",cs_fifo);

      write(client_server_fifo,sent,sizeof(*sent));

      server_client_fifo = open(sc_fifo,O_RDONLY);

      if (read(server_client_fifo,recieved,sizeof(recieved)) == -1) {
         printf("An error ocurred.\n");
      } else {
         printf("First line of the file: \n%s\n",recieved);
         close(client_server_fifo);
         close(server_client_fifo);
      }
      memset(recieved,sizeof(recieved));
   }

   return 0;
}

这里是server.c

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#define BUFSIZE 512

struct sent {
   char name[BUFSIZE];
   char fifo[BUFSIZE];
};


int main()
{
   int client_server_fifo;
   char cs_fifo[BUFSIZE] = "cs_fifo";

   int server_client_fifo;
   char sc_fifo[BUFSIZE];

   struct sent *sent;

   char name[BUFSIZE]; 
   char line[BUFSIZE]; 

   FILE *file; 

   printf("Server running...\n");
     
   mkfifo(cs_fifo,0777); 
   
   while (1)
   {
      client_server_fifo = open(cs_fifo,O_RDONLY); 

      read(client_server_fifo,sizeof(*sent)); 

      strcpy(name,sent->name);
      strcpy(sc_fifo,sent->fifo);

      if((file = fopen(name,"r")) != NULL) { 
         printf("%s\n",name);
         fgets(line,BUFSIZE,file); 
         printf("%s\n",name);
      }
    
      server_client_fifo = open(sc_fifo,O_WRONLY);

      write(server_client_fifo,line,strlen(line)); 
      
      memset(name,sizeof(name));
      memset(line,sizeof(line));

      close(client_server_fifo);

   }
   return 0;
}

为什么会发生这种情况?

解决方法

程序有未定义的行为,因为在 gthis 语句中

sprintf(sc_fifo,"sc_fifo_%d",getpid());

您正在尝试更改指针 sc_fifo 指向的字符串文字。

char *cs_fifo = "cs_fifo";

当你声明一个指向字符串文字的指针时,总是用限定符 const 声明它们。在这种情况下,如果您将托盘更改字符串文字,您将在编译时收到禁止错误。

您还使用了未初始化的指针发送

struct sent *sent;

在本声明中

read(client_server_fifo,sent,sizeof(*sent)); 

还有其他错误。例如数组没有赋值运算符。所以 client.c

中的这些语句
  sent->name = name;
  sent->fifo = cs_fifo;

不正确。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。