我将数据保存在两个不同的文件中,并使用 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的值。