-1
這是一段測試標題爲'logf'的函數的代碼,該函數接受兩個參數並使用它們記錄一些信息。在這種情況下,函數的第一個參數是要記錄的字符串,第二個參數是將文件系統中的路徑保存到日誌文件的字符串。第一個字符串用時間戳打印到stdout和日誌文件。此代碼是否包含未定義的行爲或隱藏的錯誤?我錯過了什麼?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
typedef struct tm tm;
int logf(char input_string[], char log_file_string[])
{
/*Initializations.*/
char output_string[32767];
FILE* log_file_stream;
time_t current_time_epoch_format;
tm* current_time_calandar_format;
/*Creating timestamp in output string, amending first argument to output string, and logging.*/
if ((strlen(input_string) + 23) > 32767) return EXIT_FAILURE;
if ((current_time_epoch_format = time(¤t_time_epoch_format)) == -1) return EXIT_FAILURE;
if ((current_time_calandar_format = localtime(¤t_time_epoch_format)) == NULL) return EXIT_FAILURE;
if (strftime(output_string, 23, "[%d-%m-%Y %H:%M:%S] ", current_time_calandar_format) != 22) return EXIT_FAILURE;
if ((log_file_stream = fopen(log_file_string, "a")) == NULL) return EXIT_FAILURE;
if (printf("%s\n", strcat(output_string, input_string)) < 1) return EXIT_FAILURE;
if (fprintf(log_file_stream, "%s\n", output_string) < 1) return EXIT_FAILURE;
if (fclose(log_file_stream) == EOF) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
/*Initializations.*/
int EXIT_CODE;
/*Print the returned integer from logf and exit.*/
printf("%d\n", (EXIT_CODE = logf(argv[1], argv[2])));
exit(EXIT_CODE);
}
http://sscce.org/ – StoryTeller
這屬於http://codereview.stackexchange.com/也許? –
@Cthulhu:感謝您的鏈接。 t_t – Draeton