使用 strcat 连续连接多个 char*

Concatenate multiple char* in a row using strcat

我目前正在尝试使用 strcat 在我的服务器程序中连续连接多个 'strings'。 相关部分代码如下: 我成功获取了客户端发送的年月日和文件名,因为打印的很好

我的字符串初始化如下:

    char username[MAX_USERNAME_SIZE];
    char filename[MAX_FILENAME_SIZE];
    char path[MAX_FILEPATH_SIZE];
    char buff[BUFFSIZE];
    char year[4], month[2], day[2];
    ...
    if ((numbytes = recv(new_fd, &username, MAX_USERNAME_SIZE, 0)) == -1)
    {
        perror("Serveur: recv username");
        return EXIT_FAILURE;
        }
        username[numbytes] = '[=10=]';
        printf("Serveur: username: %s\n", username); 
        /* create user's repository if it doesn't exist yet*/

        // 3) get date from client
        if ((numbytes = recv(new_fd, year, 4, 0)) == -1)
        {
            perror("Serveur: recv year");
            return EXIT_FAILURE;
        }
        year[numbytes] = '[=10=]';
        printf("year: %s\n", year);
        if ((numbytes = recv(new_fd, month, 2, 0)) == -1)
        {
            perror("Serveur: recv month");
            return EXIT_FAILURE;
        }
        month[numbytes] = '[=10=]';
        printf("month: %s\n", month);
        if ((numbytes = recv(new_fd, day, 2, 0)) == -1)
        {
            perror("Serveur: recv day");
            return EXIT_FAILURE;
        }
        day[numbytes] = '[=10=]';
        printf("day: %s\n", day);
        // get filename from client
        if ((numbytes = recv(new_fd, filename, MAX_FILENAME_SIZE, 0)) == -1)
        {
            perror("Serveur: recv filename");
            return EXIT_FAILURE;
        }
        filename[numbytes] = '[=10=]';
        printf("Serveur: filename: %s\n", filename);

但是当我尝试正确连接所有字符串时遇到问题,如图所示。

        // create user repository
        strcpy(path, argv[1]);
        printf("Serveur: Path: %s\n", path);
        strcat(path, "/");
        printf("Serveur: Path: %s\n", path);

        strcat(path, username);
        strcat(path, "/");
        printf("Serveur: Path: %s\n, username:%s\n", path, username);
        my_mkdir(path, MODE);

        strcat(path, year);
        strcat(path, "/");
        printf("Serveur: Path: %s\n, year: %s\n", path, year);
        my_mkdir(path, MODE);

        strcat(path, month);
        strcat(path, "/");
        printf("Serveur: Path: %s\n, month: %s\n", path, month);
        my_mkdir(path, MODE);

        strcat(path, day);
        strcat(path, "/");
        printf("Serveur: Path: %s\n, day: %s\n", path, day);
        my_mkdir(path, MODE);

        strcat(path, filename);
}

在我这样做之后,令人惊讶的是,用户名、年份和月份的打印效果并不好。 这是我执行代码时的输出(我确定文件名是正确的,因为我从客户端检索的文件保存在正确的名称下):

Serveur: connection recieved from client 127.0.0.1
Serveur: username: student
year: 95
month: 5
day: 11
Serveur: filename: tux.png
Serveur: Path: ./Test0/Test1
Serveur: Path: ./Test0/Test1/
Serveur: Path: ./Test0/Test1//
, username:
Serveur: Path: ./Test0/Test1///
, year: 
Serveur: Path: ./Test0/Test1////
, month: 
Serveur: Path: ./Test0/Test1////11/
, day: 11

我真的无法清楚地想象我错在哪里。提前致谢

显示显示用户名、年、月和日的内存分配的代码。确保它们没有重叠或共享。

尝试

sprintf(path,"%s/%s/%s/%s/%s/",argv[1],username,year,month,day);

或者改成这样,

 strcpy(path, argv[1]);
 printf("Serveur: Path: %s\n", path);
 strcat(path, "/");
 printf("Serveur: Path: %s\n", path);


 if ((numbytes = recv(new_fd, &username, MAX_USERNAME_SIZE, 0)) == -1){
        perror("Serveur: recv username");
        return EXIT_FAILURE;
 }
 username[numbytes] = '[=11=]';
 printf("Serveur: username: %s\n", username); 
 strcat(path, username);
 strcat(path, "/");
 printf("Serveur: Path: %s\n, username:%s\n", path, username);
 my_mkdir(path, MODE);    

 /* create user's repository if it doesn't exist yet*/

 // 3) get date from client
 if ((numbytes = recv(new_fd, year, 4, 0)) == -1){
     perror("Serveur: recv year");
     return EXIT_FAILURE;
 }
 year[numbytes] = '[=11=]';
 printf("year: %s\n", year);
 strcat(path, year);
 strcat(path, "/");
 printf("Serveur: Path: %s\n, year: %s\n", path, year);
 my_mkdir(path, MODE);

 if ((numbytes = recv(new_fd, month, 2, 0)) == -1){
     perror("Serveur: recv month");
     return EXIT_FAILURE;
 }
 month[numbytes] = '[=11=]';
 printf("month: %s\n", month);

 strcat(path, month);
 strcat(path, "/");
 printf("Serveur: Path: %s\n, month: %s\n", path, month);
 my_mkdir(path, MODE);


 if ((numbytes = recv(new_fd, day, 2, 0)) == -1){
     perror("Serveur: recv day");
     return EXIT_FAILURE;
 }
 day[numbytes] = '[=11=]';
 printf("day: %s\n", day);

 strcat(path, day);
 strcat(path, "/");
 printf("Serveur: Path: %s\n, day: %s\n", path, day);
 my_mkdir(path, MODE);

 // get filename from client
 if ((numbytes = recv(new_fd, filename, MAX_FILENAME_SIZE, 0)) == -1){
     perror("Serveur: recv filename");
     return EXIT_FAILURE;
 }
 filename[numbytes] = '[=11=]';
 printf("Serveur: filename: %s\n", filename);
 strcat(path, filename);

问题是您没有为字符串提供足够的 space。您仍然没有准确说明所有这些是如何声明的,但是有足够的示例来说明问题,例如这个。

char year[4]

上面只能容纳3个字符-第4个是space来存储[=15=]终止字符。

在这里你告诉你的代码从套接字中读取 4 个字节,它会这样做,然后 numbytes 将等于 4...

    if ((numbytes = recv(new_fd, year, 4, 0)) == -1)

...然后您使用 numbytes 添加 NUL 终止符...

    year[numbytes] = '[=12=]';

...但是 year[4] 超出了数组的范围,这会导致未定义的行为。看起来 year 中有一个有效的年份,但是随后您的代码的其余部分发生了并且发生了奇怪的事情,因为您也对所有其他字符串犯了同样的错误。

要容纳 4 个字符加上 NUL,您需要声明 year 最少为 5 个,如下所示...

char year[5]

...可是你的电脑内存这么少,需要这么节俭吗?最好给它比它需要的更多,或者将它读入一个有大量 space 的缓冲区,并分配足够的内存来创建正确大小的字符串,如果 space 非常重要,就像这个例子所示。

char *year;
char buffer[100];
if ((numbytes = recv(new_fd, buffer, 4, 0)) == -1)
{
    perror("Serveur: recv year");
    return EXIT_FAILURE;
}
buffer[numbytes] = '[=14=]';
year=malloc(numbytes+1);
strcpy(year,buffer);