#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include “aip_common.h”
#include <string.h>
#include <stdlib.h>
/===================================================================================================================================/
/* Macro Definition Area: Define constants and identifiers used in the program /
/===================================================================================================================================*/
#define ZERO (0) // Zero value constant
#define FALSE (0) // Boolean false value
#define WRONG (1) // Error status identifier
#define SUCCESS (0) // Success status identifier
#define TRUE (1) // Boolean true value
#define ENDOFSTRING (8) // Address string length
#define STORESIZE (9) // String storage buffer size
#define HEX (16) // Hexadecimal base
#define MAX_LINE_LENGTH (256) // Maximum file line length
#define ADD_VALUE (0xF) // Address increment value
#define MAX_SEARCH_STR (20) // Maximum user search string length
/===================================================================================================================================/
/* Function Declaration Area /
/===================================================================================================================================/
U1 process_search_line(S1 s1p_ap_search_line, FILE* stp_a_creatfile_write, U4* u4p_a_search_target, const S1* search_str);
U1 u1_g_OPENFile(FILE* stp_a_openfile_target);
U1 u1_g_CreatFile(FILE* stp_a_creatfile_target);
void vd_g_delet(s4_a_search_linelength, s1_ap_search_str);
void vd_g_initialize_array(U1* u1p_a_array_start, U4 u4_a_array_size, U1 u1_a_arr_value);
/===================================================================================================================================/
/* main : Program main entry function /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Open input/output files and initialize resources /
/ 2. Get user search string and process input file /
/ 3. Handle search results and write default values if target not found /
/ Parameters: /
/ void /
/ Return Value: /
/ U4: Program exit status (ZERO on success) /
/===================================================================================================================================/
U4 main(void)
{
FILE stp_t_creatfile_read; // Input file pointer
FILE* stp_t_creatfile_write; // Output file pointer
S1 s1_tp_search_line[MAX_LINE_LENGTH]; // File line buffer
S1 s1_tp_search_str[MAX_SEARCH_STR]; // User search string
S4 s4_t_search_linelength; // Line length counter
U4 u4_t_search_target; // Target search status flag
U4 u4_t_file_state;
U1 u1_t_input_state;
S4 s4_tp_store_string[STORESIZE]; // Temporary storage buffer
u4_t_search_target = (U4)FALSE; // Open input/output files stp_t_creatfile_read = fopen("DA_510B_IMG_1.mhx", "rb"); stp_t_creatfile_write = fopen("WRITE_ADDRESS.TXT", "w"); // Check file open status u1_g_OPENFile(stp_t_creatfile_read); u1_g_CreatFile(stp_t_creatfile_write); // Initialize array vd_g_initialize_array((U1*)s4_tp_store_string, STORESIZE, (U1)ZERO); // Get user input search string printf("请输入要查询的字符串: "); u1_t_input_state = fgets(s1_tp_search_str, sizeof(s1_tp_search_str), stdin); if (u1_t_input_state == NULL) { printf("读取输入失败\n"); fclose(stp_t_creatfile_read); fclose(stp_t_creatfile_write); system("pause"); u4_t_file_state = WRONG; } else { // Remove trailing newline character s4_t_search_linelength = strlen(s1_tp_search_str); vd_g_delet(s4_t_search_linelength, s1_tp_search_str); printf("输入文件打开成功\n"); printf("正在查询字符串: %s\n", s1_tp_search_str); // Process file content line by line char* u1p_t_fgetsresult = fgets(s1_tp_search_line, (U4)MAX_LINE_LENGTH, stp_t_creatfile_read); while (u1p_t_fgetsresult) { u1p_t_fgetsresult = fgets(s1_tp_search_line, (U4)MAX_LINE_LENGTH, stp_t_creatfile_read); // Remove newline character s4_t_search_linelength = strlen(s1_tp_search_line); vd_g_delet(s4_t_search_linelength, s1_tp_search_line); // Process current line and check for target if (process_search_line(s1_tp_search_line, stp_t_creatfile_write, &u4_t_search_target, s1_tp_search_str)) { break; // Break loop after finding target } } u4_t_file_state = SUCCESS; } // Cleanup resources and exit fclose(stp_t_creatfile_read); fclose(stp_t_creatfile_write); system("pause"); return (U4)u4_t_file_state;
}
/===================================================================================================================================/
/* process_search_line : Process single line of text and search for target string /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Search for target string in current line /
/ 2. Extract and process hexadecimal address if found /
/ 3. Write processed address to output file /
/ Parameters: /
/ S1* s1p_ap_search_line: Pointer to current line buffer /
/ FILE* stp_a_creatfile_write: Output file pointer /
/ U4* u4p_a_search_target: Pointer to search status flag /
/ const S1* s1p_ap_search_target: Pointer to target search string /
/ Return Value: /
/ U1: TRUE if target found, FALSE otherwise /
/===================================================================================================================================/
U1 process_search_line(S1 s1p_ap_search_line, FILE* stp_a_creatfile_write, U4* u4p_a_search_target, const S1* s1p_ap_search_target)
{
U1* u1p_t_search_target; // Target string position pointer
U1* u1p_t_search_startofline; // Start position after target
unsigned long u4_t_search_oldaddress; // Original address value
unsigned long u4_t_search_newaddress; // Processed address value
U1 u1_t_search_hexaddress[(U4)STORESIZE]; // Hexadecimal address buffer
U1 u1_t_search_state;
char s4_tp_store_string[(U4)STORESIZE]; // Temporary address string storage
// Initialize array vd_g_initialize_array((U1*)s4_tp_store_string, STORESIZE, (U1)ZERO); // Search for target string in current line u1p_t_search_target = strstr(s1p_ap_search_line, s1p_ap_search_target); if (u1p_t_search_target != NULL) { printf("找到目标行: %s\n", s1p_ap_search_line); // Calculate start position after target string u1p_t_search_startofline = u1p_t_search_target + strlen(s1p_ap_search_target); // Verify sufficient length after target if (strlen(u1p_t_search_startofline) >= (U4)ENDOFSTRING) { // Extract address string strncpy(s4_tp_store_string, u1p_t_search_startofline, (U4)ENDOFSTRING); s4_tp_store_string[(U4)ENDOFSTRING] = '\0'; printf("原始十六进制地址: %s\n", s4_tp_store_string); // Convert and process address u4_t_search_oldaddress = strtoul(s4_tp_store_string, NULL, (U4)HEX); u4_t_search_newaddress = u4_t_search_oldaddress + ADD_VALUE; // Format new address snprintf(u1_t_search_hexaddress, sizeof(u1_t_search_hexaddress), "%08lX", u4_t_search_newaddress); printf("加16后的地址: 0x%s\n", u1_t_search_hexaddress); // Write processing results fprintf(stp_a_creatfile_write, "0x40000000,0x%s\n", u1_t_search_hexaddress); fprintf(stp_a_creatfile_write, "0x43FFFFF0,0x43FFFFFF\n"); printf("已成功写入文件: WRITE_ADDRESS.TXT\n"); *u4p_a_search_target = (U4)SUCCESS; // Update search status } else { printf("错误: '%s'后字符不足8位\n", s1p_ap_search_target); } u1_t_search_state = (U4)TRUE; // Return target found } else { printf("未找到包含目标的行\n"); fprintf(stp_a_creatfile_write, "0x40000000,0x00000000\n"); fprintf(stp_a_creatfile_write, "0x43FFFFF0,0x43FFFFFF\n"); printf("已写入默认格式到文件\n"); } u1_t_search_state = (U4)FALSE; // Return target not found return u1_t_search_state;
}
/===================================================================================================================================/
/* u1_g_OPENFile : Verify successful opening of input file /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Check if input file pointer is valid /
/ Parameters: /
/ FILE* stp_a_openfile_target: Input file pointer /
/ Return Value: /
/ U1: WRONG if file open failed, no return on success /
/===================================================================================================================================/
U1 u1_g_OPENFile(FILE stp_a_openfile_target)
{
if (NULL == stp_a_openfile_target)
{
printf(“输入文件打开失败\n”);
system(“pause”);
return (U4)WRONG;
}
}
/===================================================================================================================================/
/* u1_g_CreatFile : Verify successful creation of output file /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Check if output file pointer is valid /
/ Parameters: /
/ FILE* stp_a_creatfile_target: Output file pointer /
/ Return Value: /
/ U1: WRONG if file creation failed, no return on success /
/===================================================================================================================================/
U1 u1_g_CreatFile(FILE stp_a_creatfile_target)
{
if (NULL == stp_a_creatfile_target)
{
printf(“输出文件创建失败\n”);
fclose(stp_a_creatfile_target);
system(“pause”);
return (U4)WRONG;
}
}
/===================================================================================================================================/
/* vd_g_delet : Remove trailing newline character from string /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Check and remove newline character at end of string /
/ Parameters: /
/ S4 s4_a_search_linelength: Length of input string /
/ char* s1_ap_search_str: Pointer to string buffer /
/ Return Value: /
/ void /
/===================================================================================================================================/
void vd_g_delet(S4 s4_a_search_linelength, char s1_ap_search_str)
{
if (s4_a_search_linelength > 0 && s1_ap_search_str[s4_a_search_linelength - 1] == ‘\n’)
{
s1_ap_search_str[s4_a_search_linelength - 1] = ‘\0’;
}
}
/===================================================================================================================================/
/* vd_g_initialize_array : Array initialization function /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Function: /
/ 1. Initialize each element of array to specified value /
/ Parameters: /
/ U1* array : Target array pointer /
/ U4 size : Array size /
/ U1 value : Initialization value /
/ Return Value: /
/ void /
/===================================================================================================================================/
void vd_g_initialize_array(U1 u1p_a_array_start, U4 u4_a_array_size, U1 u1_a_arr_value)
{
U4 u4_t_arrryinit_loop;
for (u4_t_arrryinit_loop = 0; u4_t_arrryinit_loop < u4_a_array_size; u4_t_arrryinit_loop++)
{
u1p_a_array_start[u4_t_arrryinit_loop] = u1_a_arr_value;
}
}
该代码的process_search_line函数有什么问题
最新发布