首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么使用fopen会出现分段错误?

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

Stack Overflow用户
提问于 2021-06-30 16:35:22
回答 1查看 48关注 0票数 0

我正在用C语言编写一个客户端-服务器模型,它使用fifos工作。我发送一个文件名和一个唯一fifo的名称,以便客户端从客户端接收数据,服务器打开文件并将其第一行写到fifo上。问题是,即使文件存在,我在打开它时也会得到一个分段错误。看起来fopen()函数起作用了,但我还是得到了错误。如果文件不存在,它只发送一个空字符串。

下面是client.c

代码语言:javascript
复制
#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", name);
      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, 0, sizeof(recieved));
   }

   return 0;
}

下面是server.c

代码语言:javascript
复制
#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, sent, 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, 0, sizeof(name));
      memset(line, 0, sizeof(line));

      close(client_server_fifo);

   }
   return 0;
}

这一切为什么要发生?

EN

回答 1

Stack Overflow用户

发布于 2021-06-30 16:39:35

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

代码语言:javascript
复制
sprintf(sc_fifo, "sc_fifo_%d", getpid());

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

代码语言:javascript
复制
char *cs_fifo = "cs_fifo";

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

另外,您正在使用未初始化的指针发送

代码语言:javascript
复制
struct sent *sent;

在此语句中

代码语言:javascript
复制
read(client_server_fifo, sent, sizeof(*sent)); 

还有其他错误。例如,数组没有赋值运算符。因此client.c中的这些语句

代码语言:javascript
复制
  sent->name = name;
  sent->fifo = cs_fifo;

是不正确的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68191208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档