Strcat 结果具有来自上次 strcat 调用的数据

我将数据保存在两个不同的文件中,并使用 strcat 连接这些数据。 奇怪的是,我最后一次 strcat 调用的结果将连接到我现在想要连接的两个字符串。 它可能有点模糊,所以这里是代码:

...
strcat(logline,"\n");
if(logging){
    if(writeInFile(logfile,"a",logline))
         printf("    Connection logged.");
    else
         printf("    Connection couldn't be logged.");        
}

if(saving){
    char* loc = (char*) malloc(BUFSIZ);
    strcat(loc,client_ip);
    strcat(loc,"-");
    strcat(loc,server_ip);
    strcat(loc,".txt");

    if(writeInFile(loc,request)){
          printf("    Connection saved.");
    }
    else{
          printf("ERROR: cannot create/open savefile %s\n",loc);
          printf("Saving set to FALSE.");
          saving = false;
    }
}
bool writeInFile(char* fileName,char* openingParam,char* content){
    if(strcmp(openingParam,"a") == 0 || strcmp(openingParam,"w") == 0){
        FILE* fptr = NULL;
        fptr = fopen(fileName,openingParam);
        if ( fptr == NULL)
        {
            printf("ERROR: cannot create/open logfile %s\n",fileName);
            return false;
        }
        fprintf(fptr,"%s",content);
        fclose(fptr);
        return true;
    }
    
    return false;
}

发生的事情是日志行的内容放在 loc 的开头。 这样就创建了一个名字很长的文件。

EDIT :: 该文件应该命名为 192.168.1.36-192.168.1.36.txt

而是改为命名为

|--> timestamp = Sat Jan  2 20:09:24 2021
|--> remote    = 192.168.1.36
|--> local     = 192.168.1.36
|--> request   = [timeout]
|--> END
192.168.1.36-192.168.1.36.txt
|--> timestamp = Sat Jan  2 20:09:24 2021
|--> remote    = 192.168.1.36
|--> local     = 192.168.1.36
|--> request   = [timeout]
|--> END

是通过strcat获得的logline的值。

windyleou 回答:Strcat 结果具有来自上次 strcat 调用的数据

strcat 函数要求目标字符串确实是一个正确的以空字符结尾的字符串。否则会导致未定义行为

您使用 malloc 分配的缓冲区未以任何方式初始化。它绝对不是以空字符结尾的字符串。

您有四种可能的解决方案:

  1. 在第一次调用时使用 strcpy 而不是 strcat

    strcpy(loc,client_ip);
    
  2. 初始化缓冲区,例如像这样:

    loc[0] = '\0';  // Terminate the buffer,making it an "empty" string
    strcat(loc,client_ip);
    
  3. 调用 calloc 而不是 malloc,因为这会将分配的内存归零,这与将其全部设置为字符串空终止符相同:

    char* loc = calloc(BUFSIZ,1);
    
  4. 使用 snprintf 将字符串“打印”到未初始化的缓冲区中:

    snprintf(loc,BUFSIZ,"%s-%s.txt",client_ip,server_ip);
    

我个人推荐方法 4,使用 snprintf


您的代码还有另一个问题:内存泄漏,因为您没有传递分配给 free 的内存。

要么在 free(loc) 超出范围之前调用 loc;或者将loc改为数组:

char loc[BUFSIZ];

loc 改为数组也意味着您可以轻松地对其进行初始化:

char loc[BUFSIZ] = { '\0' };
,

Loc 未初始化,带有 uninit var 的 strcat 行为未定义,这就是问题所在。 用strcpy替换第一次出现的strcat就解决了!

if(saving){
    char* loc = (char*) malloc(BUFSIZ);
    strcpy(loc,client_ip);
    strcat(loc,"-");
    strcat(loc,server_ip);
    strcat(loc,".txt");

    if(writeInFile(loc,"a",request)){
          printf("    Connection saved.");
    }
    else{
          printf("ERROR: cannot create/open savefile %s\n",loc);
          printf("Saving set to FALSE.");
          saving = false;
    }
}
本文链接:https://www.f2er.com/1166553.html

大家都在问