gpt4 book ai didi

c++ - Parsing variable and segmentation fault (core dumped) 错误

转载 作者:行者123 更新时间:2023-11-30 03:48:54 26 4
gpt4 key购买 nike

我遇到了一个问题,将用户字符串作为解析的变量传递进来,以便将每个单独的单词存储在它自己的变量中。以下是我对此的尝试。

client.c 中还有程序的另一部分请求用户输入。一旦我在客户端“foo 123 anwhere blah”中输入一个字符串,它就会作为变量 buf 传递到下面的服务器程序中。获得它后,我将其复制到 temp 中,这样我就不会损坏原始字符串(至少在测试的这一点上是这样)。然后我针对尝试解析每个单词的变量运行 strctok

如果此刻我什至走在正确的轨道上,它将编译无误并运行。但是,当我从客户端向它发送数据时,会出现“段错误(核心已转储)”。

任何人都可以阐明原因吗?

        /*
* server.c
*/

#include <stdio.h>
#include <iostream>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cstring>
#include <cstdlib>

using namespace std;

#define SERVER_PORT 1617
#define MAX_PENDING 5
#define MAX_LINE 256

int main(int argc, char **argv) {

struct sockaddr_in sin;
socklen_t addrlen;
char buf[MAX_LINE];
int len;
int s;
int new_s;
char *temp;
char fname[32], lname[32], city[32], zip[32], country[32];

/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (SERVER_PORT);

/* setup passive open */
if (( s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}

if ((bind(s, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
perror("bind");
exit(1);
}

listen (s, MAX_PENDING);

addrlen = sizeof(sin);
cout << "The server is up, waiting for connection" << endl;

/* wait for connection, then receive and print text */
while (1) {
if ((new_s = accept(s, (struct sockaddr *)&sin, &addrlen)) < 0) {
perror("accept");
exit(1);
}
cout << "new connection from " << inet_ntoa(sin.sin_addr) << endl;

while (len = recv(new_s, buf, sizeof(buf), 0)) {

temp = buf;
strcpy(fname, strtok(buf , " "));
strcpy(lname, strtok(NULL, " "));
strcpy(city , strtok(NULL, " "));
strcpy(zip , strtok(NULL, " "));
strcpy(country, strtok(NULL, " "));


printf("%s\n", fname);
printf("%s\n", lname);
printf("%s\n", city);
printf("%s\n", zip);
printf("%s\n", country);


/* send (new_s, temp, strlen(temp) + 1, 0); */
}

close(new_s);
}
}

根据 gcc 上的 -g 选项的要求:

server.c:6:20: error: iostream: No such file or directory

server.c:13:19: error: cstring: No such file or directory

server.c:14:19: error: cstdlib: No such file or directory

server.c:16: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before ânamespaceâ

server.c: In function âmainâ:

server.c:42: warning: incompatible implicit declaration of built-in function âexitâ

server.c:47: warning: incompatible implicit declaration of built-in function âexitâ

server.c:53: error: âcoutâ undeclared (first use in this function)

server.c:53: error: (Each undeclared identifier is reported only once

server.c:53: error: for each function it appears in.)

server.c:53: error: âendlâ undeclared (first use in this function)

server.c:59: warning: incompatible implicit declaration of built-in function âexitâ

server.c:65: warning: incompatible implicit declaration of built-in function âmemcpyâ

server.c:66: warning: incompatible implicit declaration of built-in function âstrcpyâ

server.c:66: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast

server.c:66: note: expected âconst char *â but argument is of type âintâ

server.c:67: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast

server.c:67: note: expected âconst char *â but argument is of type âintâ

server.c:68: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast

server.c:68: note: expected âconst char *â but argument is of type âintâ

server.c:69: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast

server.c:69: note: expected âconst char *â but argument is of type âintâ

server.c:70: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast

server.c:70: note: expected âconst char *â but argument is of type âintâ

最佳答案

将 strcpy 替换为 strncpy。并根据要复制的缓冲区的长度在 strncpy 中使用正确的长度。你也没有检查 strtok 的返回值。由于 strtok 可以返回 NULL,因此您正在尝试执行类似 strcpy(str, null) 的操作。

关于c++ - Parsing variable and segmentation fault (core dumped) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com