size_t(-1) / sizeof(_Tp)到底是何意义?

本文解释了size_t(-1)/sizeof(_Tp)在32位和64位系统中所代表的意义,即实现/平台允许的最大容器元素数量。同时介绍了size_t(-1)为何等于2^32(或2^64),并讨论了如何利用这些信息来确定容器如std::vector<T>的最大容量。

Ø  size_t(-1)/sizeof(_Tp)到底是什么意义?

32位机来说,它代表了实现/平台允许你放入容器的最大元素数目,对于std::vector<T>最大容量为2^32 / sizeof(T) 个元素。换句话来说, 也就是我能完全使用4GiB虚拟内存地址空间(实际不可能)而填充的最大元素数目。

Ø  为什么size_t(-1)2^32

size_t通常在32位机上定义为32位,而在64位机上定义为64位的无符号整型。通过强制类型转换,size_t(-1)恰能代表其能表示的最大值,即2^32 (2^64).

 

英文资料:Type size_t 来源:维基百科 http://en.wikipedia.org/wiki/Stddef.h

The type size_t represents the appropriate type for representing the size of objects of memory areas, and for use in dereferencing the elements of an array. It has an implementation-dependent size; usually but not necessarily, it has a 32-bit representation on 32-bit systems and a 64-bit representation on 64-bit systems. It is unsigned.

 

 

 

 

 

make "make -C modules/arp_scanner MODULE_NAME=arp_scanner" make[4]: Entering directory '/home/fuyu1/code/Platform_NVMP/nvmp/build_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/nsd/modules/arp_scanner' arm-ca9-linux-uclibcgnueabihf-gcc -O2 -pipe -fgnu89-inline -march=armv7-a -mtune=cortex-a9 -mfpu=neon -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -g -Wno-stringop-truncation -Wno-format-truncation -Wno-sizeof-pointer-div -Wno-stringop-overflow -Wno-format-overflow -Wno-sizeof-pointer-memaccess -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/usr/lib/libiconv-full/include -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/usr/lib/libintl-full/include -DCONFIG_STATISTIC_REPORT_DOMAIN=n-da.tplinkcloud.com.cn -Wno-sizeof-pointer-div -Wno-restrict -Wno-format-truncation -Wno-format-overflow -Wno-stringop-truncation -DSUPPORT_PLUGIN -DCONFIG_MSG_PUSH_POST_URL=/surveillance/v1/reportMsg -DCONFIG_NTP_HOSTNAME=n-tss.tplinkcloud.com.cn -DUP_FIRMWARE_LIMIT_SIZE=16777216 -DSENSITIVITY_INT -DMAKEROOM_BEFORE_UPGRADE -DAUDIO_ENABLE -DCONFIG_TP_TAPO_MAP_ROOTFS -DTP_VIGI -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/usr/include -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/include -I/home/fuyu1/code/Platform_NVMP/nvmp/../sdk/soc/nvt9856x/uclibc-toolchain-1.0.32/arm-ca9-linux-uclibcgnueabihf-8.4.01/arm-ca9-linux-uclibcgnueabihf/sysroot/usr/include -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/usr/lib/libiconv-full/include -I/home/fuyu1/code/Platform_NVMP/nvmp/staging_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/usr/lib/libintl-full/include -Wall -Werror -ffunction-sections -fdata-sections -DMODULE_LIST="\"tdpd tmpd mactool nifc ipcd dhcpc diagnose httpd tpntp system upgrade arp_scanner\"" -I/home/fuyu1/code/Platform_NVMP/nvmp/build_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/nsd/include -I/home/fuyu1/code/Platform_NVMP/nvmp/build_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/nsd/common -I/home/fuyu1/code/Platform_NVMP/nvmp/build_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/nsd/common/ds -I/home/fuyu1/code/Platform_NVMP/nvmp/build_dir/target-arm-ca9-linux-uclibcgnueabihf-cx20iv1.20/nsd/modules/mactool -I./libXml -I./libutils -I./libmediautil -I../../include -c -o arp_scan.o arp_scan.c arp_scan.c:7:18: error: 'g_arp_send_pt' initialized and declared 'extern' [-Werror] extern pthread_t g_arp_send_pt = 0; ^~~~~~~~~~~~~ arp_scan.c:8:18: error: 'g_arp_recv_pt' initialized and declared 'extern' [-Werror] extern pthread_t g_arp_recv_pt = 0; ^~~~~~~~~~~~~ arp_scan.c:9:12: error: 'g_arp_send_thread_flag' initialized and declared 'extern' [-Werror] extern int g_arp_send_thread_flag = 0; ^~~~~~~~~~~~~~~~~~~~~~ arp_scan.c:10:12: error: 'g_arp_recv_thread_flag' initialized and declared 'extern' [-Werror] extern int g_arp_recv_thread_flag = 0; ^~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors <builtin>: recipe for target 'arp_scan.o' failed make[4]: *** [arp_scan.o] Error 1
09-02
S32 origin_info_init(ORIGIN_FW_INFO *origin_info) { TP_HEAD_INFO origin_tp_head; FACTORY_INFO_DATA fac_info; U32 size; S32 ret; #ifdef CONFIG_LTE_MODULE_IN_UP_BIN MODULE_SPEC module_spec; LTE_PARAM_MODULE_INFO lte_module_info; #endif if (NULL == origin_info) { goto INIT_ERROR; } memset(origin_info, 0, sizeof(ORIGIN_FW_INFO)); memset(&origin_tp_head, 0, sizeof(TP_HEAD_INFO)); memset(&fac_info, 0, sizeof(FACTORY_INFO_DATA)); #ifdef CONFIG_LTE_MODULE_IN_UP_BIN memset(&module_spec, 0, sizeof(MODULE_SPEC)); memset(&lte_module_info, 0, sizeof(LTE_PARAM_MODULE_INFO)); #endif /* 读取 TP HEAD */ size = ds_read(TP_HEADER_PATH, &origin_tp_head, sizeof(TP_HEAD_INFO)); if (size < sizeof(TP_HEAD_INFO)) { UPGRADE_ERROR("Reading TP_HEAD failed."); goto INIT_ERROR; } /* 读取 fac_info */ size = ds_read(FACTORY_INFO_DATA_PATH, &fac_info, sizeof(FACTORY_INFO_DATA)); if (size < sizeof(FACTORY_INFO_DATA)) { UPGRADE_ERROR("Reading FACTORY_INFO_DATA failed."); goto INIT_ERROR; } /* init local partition offsets and lens */ origin_info->tphead_len = ntohl(origin_tp_head.tphead_len); #ifdef FDT_UPGRADE_SUPPORT origin_info->fdt_offset = ntohl(origin_tp_head.fdt_offset); //fdt origin_info->fdt_len = ntohl(origin_tp_head.fdt_len); #else origin_info->fdt_offset = 0; //fdt origin_info->fdt_len = 0; #endif origin_info->uboot_offset = ntohl(origin_tp_head.uboot_offset); origin_info->uboot_len = ntohl(origin_tp_head.uboot_len); origin_info->tphead_offset = ntohl(origin_tp_head.tphead_offset); origin_info->kernel_offset = ntohl(origin_tp_head.kernel_offset); origin_info->vendor_id = ntohs(origin_tp_head.vendor_id); origin_info->zone_code = ntohs(origin_tp_head.zone_code); /* init local partition CRCs */ #ifdef FDT_UPGRADE_SUPPORT memcpy(origin_info->fdt_crc, origin_tp_head.fdt_crc, CRC_LEN); //fdt #endif memcpy(origin_info->factory_info_crc, origin_tp_head.factory_info_crc, CRC_LEN); memcpy(origin_info->radio_crc, origin_tp_head.radio_crc, CRC_LEN); memcpy(origin_info->bootloader_crc, origin_tp_head.uboot_crc, CRC_LEN); memcpy(origin_info->kernel_and_romfs_crc, origin_tp_head.firmware_crc, CRC_LEN); memcpy(origin_info->fw_id, origin_tp_head.fw_id, FW_ID_LEN); /* get BL FwID number and init BL */ origin_info->bl_num = ntohl(origin_tp_head.fw_id_bl_num); memcpy(origin_info->bl_id, origin_tp_head.fw_id_bl, FW_ID_LEN * origin_info->bl_num); /* init hwid */ ret = hex_str_to_bytes(origin_info->hw_id, (char*)fac_info.hw_id, HW_ID_LEN); if (0 != ret) { goto INIT_ERROR; } /* init mac */ memcpy(origin_info->mac, fac_info.mac, MAC_ADDR_SIZE); /* init extFw dev_name: dev_name + _ + hw_ver */ strcpy((char*)origin_info->dev_name, (char*)fac_info.dev_name); size = strlen(fac_info.dev_name); *(origin_info->dev_name + size) = '_'; strcpy((char*)origin_info->dev_name + size + 1, (char*)fac_info.hw_ver); #ifdef CONFIG_LTE_MODULE_IN_UP_BIN /* init lte info */ size = ds_read(MODULE_SPEC_PATH, &module_spec, sizeof(MODULE_SPEC)); if (size < sizeof(MODULE_SPEC)) { UPGRADE_ERROR("Reading module_spec failed."); goto INIT_ERROR; } origin_info->lte_supported = atoi(module_spec.lte); if (origin_info->lte_supported) { size = ds_read(LTE_MODULE_INFO_PATH, &lte_module_info, sizeof(LTE_PARAM_MODULE_INFO)); if (size < sizeof(LTE_PARAM_MODULE_INFO)) { UPGRADE_ERROR("Reading lte module info failed."); goto INIT_ERROR; } strncpy((char *)origin_info->lte_vender, (const char *)lte_module_info.vender, sizeof(origin_info->lte_vender)); strncpy((char *)origin_info->lte_product, (const char *)lte_module_info.product, sizeof(origin_info->lte_product)); origin_info->lte_version = atoi(lte_module_info.version); } #endif return SLP_ENONE; INIT_ERROR: return SLP_ESYSTEM; }
最新发布
09-06
#include <stdio.h> #include <windows.h> #include "time_display.h" #define MAX_TIME_STR (256) // 时间字符串最大长度 #define MAX_WEEK (7) #define MAX_MONTH (12) #define TIME_HALFDAY (12) #define TEST_ZERO (0) #define FORMAT_ERROR (0) #define TEST_ONE (1) #define TEST_FIVE (5) #define CONDITIONONE (1) #define CONDITIONTWO (2) #define CONDITIONTHREE (3) #define CONDITIONFOUR (4) #define CONDITIONFIVE (5) static const U1* u1p_sp_TIME_WEEKZH[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; static const U1* u1p_sp_TIME_WEEKEN[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; static const U1* u1p_sp_TIME_WEEKJP[] = { "日", "月", "火", "水", "木", "金", "土" }; static const U1* u1p_sp_TIME_MONTHEN[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; static U4 u4_s_time_Format(U1 u1_a_format_index, SYSTEMTIME* stp_a_time_t, U1* u1p_a_time_bufferone, size_t u4_a_buf_size) { U4 u4_t_format_ret ; // 默认返回错误值 U2 u2_t_time_y; S1 s1_t_time_m; S1 s1_t_time_d; S1 s1_t_time_h24; S1 s1_t_time_min; S1 s1_t_time_sec; S1 s1_t_time_w; S1 s1_t_time_h12; const S1* s1p_t_TIME_AMPM; if (!u1p_a_time_bufferone || u4_a_buf_size < (U4)MAX_TIME_STR || !stp_a_time_t) { u4_t_format_ret= (U4)FORMAT_ERROR; // 直接返回错误 } else { // 时间变量提取 u2_t_time_y = (U2)stp_a_time_t->wYear; s1_t_time_m = (S1)stp_a_time_t->wMonth; s1_t_time_d = (S1)stp_a_time_t->wDay; s1_t_time_h24 = (S1)stp_a_time_t->wHour; s1_t_time_min = (S1)stp_a_time_t->wMinute; s1_t_time_sec = (S1)stp_a_time_t->wSecond; s1_t_time_w = (S1)stp_a_time_t->wDayOfWeek; if ((U1)TEST_ZERO == s1_t_time_h24 % (U1)TIME_HALFDAY) { s1_t_time_h12 = (U1)TIME_HALFDAY; } else { s1_t_time_h12 = s1_t_time_h24 % (U1)TIME_HALFDAY; } if (s1_t_time_h24 >= (U1)TIME_HALFDAY) { s1p_t_TIME_AMPM = "PM"; } else { s1p_t_TIME_AMPM = "AM"; } // 格式选择 switch (u1_a_format_index) { case (U4)CONDITIONONE: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式1:%u年%u月%u日 %s %u点%u分%u秒", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONTWO: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式2:%u/%u/%u %s %02u:%02u:%02u", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONTHREE: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式3:%u/%u/%u %s %u:%02u:%02u(%s)", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM); break; case (U4)CONDITIONFOUR: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式4:%s, %s %u, %u %02u:%02u:%02u", u1p_sp_TIME_WEEKEN[s1_t_time_w], u1p_sp_TIME_MONTHEN[s1_t_time_m - 1], s1_t_time_d, u2_t_time_y, s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONFIVE: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式5:%u年%u月%u日(%s) %u:%02u:%02u(%s)", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKJP[s1_t_time_w], s1_t_time_h12, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM); break; default: u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "[错误] 无效格式编号:%u", u1_a_format_index); break; } } return u4_t_format_ret; } // 显示指定格式的时间(使用核心格式化函数) void vd_g_time_Display_format(U1 u1_a_format_index) { SYSTEMTIME st_t_format_t; GetLocalTime(&st_t_format_t); U1 u1_tp_format_buffer[(U4)MAX_TIME_STR]; u4_s_time_Format(u1_a_format_index, &st_t_format_t, u1_tp_format_buffer, sizeof(u1_tp_format_buffer)); printf("%s\n", u1_tp_format_buffer); // 添加换行符 } // 显示所有格式的时间 void vd_g_time_Display_allformats(void) { U1 u1_t_allformats_i; for (u1_t_allformats_i = (U1)TEST_ONE; u1_t_allformats_i <= (U1)TEST_FIVE; ++u1_t_allformats_i) { vd_g_time_Display_format(u1_t_allformats_i); } } // 写入所有格式到文件(使用核心格式化函数) void vd_g_time_Write_allformats_to_file(void) { FILE* stp_t_file_fp; errno_t s4_t_file_err; SYSTEMTIME st_t_systemtime_t; U1 u1_t_file_i; U1 u1_tp_file_buffer[(U4)MAX_TIME_STR]; S4 s4_t_fclose_result; // 正确使用 fopen_s 需要三个参数:文件指针地址、文件名、打开模式 s4_t_file_err = fopen_s(&stp_t_file_fp, "time_log.txt", "a"); if ((S4)TEST_ONE!= s4_t_file_err || NULL == stp_t_file_fp) { printf("[错误] 无法打开 time_log.txt,错误码:%d\n", s4_t_file_err); return; } else { /*do nothing*/ } GetLocalTime(&st_t_systemtime_t); for (u1_t_file_i = 1; u1_t_file_i <= 5; ++u1_t_file_i) { u4_s_time_Format(u1_t_file_i, &st_t_systemtime_t, u1_tp_file_buffer, sizeof(u1_tp_file_buffer)); fprintf(stp_t_file_fp, "%s\n", u1_tp_file_buffer); // 添加换行符 } fprintf(stp_t_file_fp, "----------------------------------------\n\n"); // 增强分隔符 s4_t_fclose_result = fclose(stp_t_file_fp); if ((S4)TEST_ZERO != s4_t_fclose_result) { printf("[警告] 文件关闭失败\n"); } else { /*do nothing*/ } }添加英文注释,用/* */单行注释
08-06
#include <stdio.h> #include <windows.h> #include "time_display.h" #define MAX_TIME_STR (256) /* Maximum length of time string */ #define MAX_WEEK (7) /* Number of days in a week */ #define MAX_MONTH (12) /* Number of months in a year */ #define TIME_HALFDAY (12) /* Half-day point for AM/PM conversion */ #define TEST_ZERO (0) /* Zero value constant */ #define FORMAT_ERROR (0) /* Error code for format failure */ #define TEST_ONE (1) /* Numeric constant one */ #define TEST_FIVE (5) /* Numeric constant five */ #define CONDITIONONE (1) /* Format index constant 1 */ #define CONDITIONTWO (2) /* Format index constant 2 */ #define CONDITIONTHREE (3) /* Format index constant 3 */ #define CONDITIONFOUR (4) /* Format index constant 4 */ #define CONDITIONFIVE (5) /* Format index constant 5 */ /* Chinese weekday names */ static const U1* u1p_sp_TIME_WEEKZH[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; /* English weekday names */ static const U1* u1p_sp_TIME_WEEKEN[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; /* Japanese weekday abbreviations */ static const U1* u1p_sp_TIME_WEEKJP[] = { "日", "月", "火", "水", "木", "金", "土" }; /* English month names */ static const U1* u1p_sp_TIME_MONTHEN[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /* Format time according to specified index */ static U4 u4_s_time_Format(U1 u1_a_format_index, SYSTEMTIME* stp_a_time_t, U1* u1p_a_time_bufferone, size_t u4_a_buf_size) { U4 u4_t_format_ret; /* Return value storage */ U2 u2_t_time_y; /* Year component */ S1 s1_t_time_m; /* Month component */ S1 s1_t_time_d; /* Day component */ S1 s1_t_time_h24; /* Hour in 24-hour format */ S1 s1_t_time_min; /* Minute component */ S1 s1_t_time_sec; /* Second component */ S1 s1_t_time_w; /* Weekday index */ S1 s1_t_time_h12; /* Hour in 12-hour format */ const S1* s1p_t_TIME_AMPM; /* AM/PM indicator */ /* Validate input parameters */ if (!u1p_a_time_bufferone || u4_a_buf_size < (U4)MAX_TIME_STR || !stp_a_time_t) { u4_t_format_ret = (U4)FORMAT_ERROR; /* Return error on invalid input */ } else { /* Extract time components from SYSTEMTIME structure */ u2_t_time_y = (U2)stp_a_time_t->wYear; s1_t_time_m = (S1)stp_a_time_t->wMonth; s1_t_time_d = (S1)stp_a_time_t->wDay; s1_t_time_h24 = (S1)stp_a_time_t->wHour; s1_t_time_min = (S1)stp_a_time_t->wMinute; s1_t_time_sec = (S1)stp_a_time_t->wSecond; s1_t_time_w = (S1)stp_a_time_t->wDayOfWeek; /* Convert 24-hour to 12-hour format */ if ((U1)TEST_ZERO == s1_t_time_h24 % (U1)TIME_HALFDAY) { s1_t_time_h12 = (U1)TIME_HALFDAY; } else { s1_t_time_h12 = s1_t_time_h24 % (U1)TIME_HALFDAY; } /* Determine AM/PM indicator */ if (s1_t_time_h24 >= (U1)TIME_HALFDAY) { s1p_t_TIME_AMPM = "PM"; } else { s1p_t_TIME_AMPM = "AM"; } /* Select format based on index */ switch (u1_a_format_index) { case (U4)CONDITIONONE: /* Chinese format with weekday */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式1:%u年%u月%u日 %s %u点%u分%u秒", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONTWO: /* Numeric date with Chinese weekday */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式2:%u/%u/%u %s %02u:%02u:%02u", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONTHREE: /* Numeric date with AM/PM */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式3:%u/%u/%u %s %u:%02u:%02u(%s)", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKZH[s1_t_time_w], s1_t_time_h24, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM); break; case (U4)CONDITIONFOUR: /* English full date format */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式4:%s, %s %u, %u %02u:%02u:%02u", u1p_sp_TIME_WEEKEN[s1_t_time_w], u1p_sp_TIME_MONTHEN[s1_t_time_m - 1], s1_t_time_d, u2_t_time_y, s1_t_time_h24, s1_t_time_min, s1_t_time_sec); break; case (U4)CONDITIONFIVE: /* Japanese-style format */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "格式5:%u年%u月%u日(%s) %u:%02u:%02u(%s)", u2_t_time_y, s1_t_time_m, s1_t_time_d, u1p_sp_TIME_WEEKJP[s1_t_time_w], s1_t_time_h12, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM); break; default: /* Handle invalid format index */ u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size, "[错误] 无效格式编号:%u", u1_a_format_index); break; } } return u4_t_format_ret; /* Return formatted string length */ } /* Display time in specified format */ void vd_g_time_Display_format(U1 u1_a_format_index) { SYSTEMTIME st_t_format_t; /* System time structure */ GetLocalTime(&st_t_format_t); /* Get current local time */ U1 u1_tp_format_buffer[(U4)MAX_TIME_STR]; /* Format buffer */ /* Format and print time */ u4_s_time_Format(u1_a_format_index, &st_t_format_t, u1_tp_format_buffer, sizeof(u1_tp_format_buffer)); printf("%s\n", u1_tp_format_buffer); /* Print with newline */ } /* Display all available time formats */ void vd_g_time_Display_allformats(void) { U1 u1_t_allformats_i; /* Loop counter */ /* Iterate through all format options */ for (u1_t_allformats_i = (U1)TEST_ONE; u1_t_allformats_i <= (U1)TEST_FIVE; ++u1_t_allformats_i) { vd_g_time_Display_format(u1_t_allformats_i); /* Display each format */ } } /* Write all time formats to log file */ void vd_g_time_Write_allformats_to_file(void) { FILE* stp_t_file_fp; /* File pointer */ errno_t s4_t_file_err; /* File error code */ SYSTEMTIME st_t_systemtime_t; /* System time structure */ U1 u1_t_file_i; /* Loop counter */ U1 u1_tp_file_buffer[(U4)MAX_TIME_STR]; /* Format buffer */ S4 s4_t_fclose_result; /* File close result */ /* Open log file in append mode */ s4_t_file_err = fopen_s(&stp_t_file_fp, "time_log.txt", "a"); if ((S4)TEST_ONE != s4_t_file_err || NULL == stp_t_file_fp) { printf("[错误] 无法打开 time_log.txt,错误码:%d\n", s4_t_file_err); return; } else { /* No action needed on success */ } GetLocalTime(&st_t_systemtime_t); /* Get current time */ /* Write all formats to file */ for (u1_t_file_i = 1; u1_t_file_i <= 5; ++u1_t_file_i) { u4_s_time_Format(u1_t_file_i, &st_t_systemtime_t, u1_tp_file_buffer, sizeof(u1_tp_file_buffer)); fprintf(stp_t_file_fp, "%s\n", u1_tp_file_buffer); /* Write with newline */ } /* Add separator between entries */ fprintf(stp_t_file_fp, "----------------------------------------\n\n"); /* Close file handle */ s4_t_fclose_result = fclose(stp_t_file_fp); if ((S4)TEST_ZERO != s4_t_fclose_result) { printf("[警告] 文件关闭失败\n"); /* Warn on close failure */ } else { /* No action needed on success */ } } /*===================================================================================================================================*/ /* Display Application Opening2 : Post Task */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Arguments: U1 u1_contents : Selected Content ID in dspmgr */ /* Return: - */ /*===================================================================================================================================*/按照这个格式帮我注释函数,原来的注释不要删了
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值