与上述功能相关的操作遇到困难。不过,我可以使用
printf();
函数打印输出。void *problematic_write (void *arg) {
char *host_match =(char *)arg;
FILE *tmp_host = fopen("../tmp.txt", "arw+");
if (tmp_host == NULL) {
printf("Something went wrong creating file!");
exit(EXIT_FAILURE);
}
printf("%s", host_match);
fprintf(tmp_host, "%s", host_match);
}
早些时候...
start_index = match_2[0].rm_so;
host_len = (match_2[0].rm_eo) - start_index;
host_match = (char * )malloc(host_len);
sprintf(host_match, "%.*s", host_len - 6, &clientbuffer_main[start_index + 6]);
host_match[host_len - 6] = '\0';
pthread_create(&thread_id, NULL, problematic_write, (void *)host_match);
pthread_join(thread_id, NULL);
基本上我正在提取http请求的
Host:
部分。
输出:
localhost:8080
但没有任何内容写入文件!
这只是我遇到问题的操作之一的示例。
标准未定义文件打开模式“arw+”。除非实现定义了这种情况下的行为,否则使用此模式是未定义的行为。
使用
fwrite()
写入文件并不一定会导致给定数据立即写入文件。相反,文件流通常会被缓冲,以便数据首先写入 RAM 中的缓冲区。在某些情况下,缓冲区内容会写入实际文件。这可以通过调用 fflush()
或 fclose()
来强制执行。
函数被声明为返回
void *
类型的指针。但是,没有返回语句。
使用OP功能
problematic_write()
中的文件,以模式“a”打开就足够了。此外,函数结束时不会返回或保存打开文件的句柄。因此,返回之前必须关闭该文件。最后,该函数必须声明为 void
(或返回一个值):
void problematic_write (void *arg) {
char *host_match =(char *)arg;
FILE *tmp_host = fopen("../tmp.txt", "a"); // Open file with a defined mode
if (tmp_host == NULL) {
printf("Something went wrong creating file!");
exit(EXIT_FAILURE);
}
printf("%s", host_match);
fprintf(tmp_host, "%s", host_match);
fclose(tmp_host); // Close the file
}