C language text parsing program & sscanf/sprintf

Source: Internet
Author: User

In many cases, the software uses parsing a text file, such as parsing a TXT file, reading useful data and processing it; or reading a stream file to find out the corresponding value. Example:

// R, GR, GB, B per light source

0f12 4819 },
/* Clock0, system clock 58 MHz, PVI clock 48 MHz, (PreView )*/

0f12 f000 //, 16, 0 },
P10

0f12 fa05 //, 16, 0 },

As shown above, the text structure is a sequence consisting of register addresses and values. There are annotation lines and delayed lines starting with P. The program needs to read the address, data, and latency to debug some devices online. The procedure is as follows:

Uint16 init_reg [4000] = {0}; // register array. Assume that there are 4000 registers to be written into uint16 init_val [4000] = {0 }; // array of parameter values char * curr_ptr = NULL; // char * Buf; // Buf indicates the first memory pointer curr_ptr = Buf after opening the file; // search for while (curr_ptr <(BUF + file_size) from the file header) // The file is not completely traversed {While (* curr_ptr = '') | (* curr_ptr = '\ t')/* Skip Space & tab */curr_ptr ++ at the beginning of each line; If (* curr_ptr) = '/') & (* (curr_ptr + 1) = '*') {While (! (* Curr_ptr) = '*') & (* (curr_ptr + 1) = '/') {curr_ptr ++; /* Skip block comment code. */} while (! (* Curr_ptr = 0x0d) & (* (curr_ptr + 1) = 0x0a) // wrap {curr_ptr ++;} curr_ptr + = 2; /* skip the enter line */continue; // start the search for the next row} If (* curr_ptr) = '/') & (* (curr_ptr + 1) = '/')/* Comment line, skip it. */{While (! (* Curr_ptr = 0x0d) & (* (curr_ptr + 1) = 0x0a) {curr_ptr ++;} curr_ptr + = 2; /* skip the enter line */continue; // start the next line}/* This just content one enter line. */If (* curr_ptr) = 0x0d) & (* (curr_ptr + 1) = 0x0a )) // call a separate empty row {curr_ptr + = 2; continue;} If ('p' = * curr_ptr) // delay latency {init_reg [I] = 0 xfcfc; // latency register curr_ptr ++; init_val [I] = simple_strtol (curr_ptr, null, 10 ); // Convert the string to data in decimal format and assign it to init_val [I]} else {init_reg [I] = simple_strtol (curr_ptr, null, 16 ); // convert the string to an address in hexadecimal format and assign it to init_reg [I] curr_ptr + = 4; // skip the 4 hexadecimal address char C; do {c = * curr_ptr; If ('0' <= (c) & (c) <= '9') | ('A' <= (c) & (c) <= 'F') | ('A' <= (c) & (c) <= 'F') {break; // skip invalid characters until the parameter value is met} curr_ptr ++;} while (1); init_val [I] = simple_strtol (curr_ptr, null, 16 ); // convert the string to a parameter in hexadecimal notation and assign it to init_val [I]} I ++; // quasi Backup Data to be written in the next row/* Skip to next line directly. */while (! (* Curr_ptr = 0x0d) & (* (curr_ptr + 1) = 0x0a) {curr_ptr ++ ;}curr_ptr + = 2; // convert to the next row}

After traversing, you can get the address and parameters to be written from init_reg and init_val.
========================================================== ========================================================== ==========================================

Typical sscanf/sprintf usage:

Sprintf (& szbuffer [strlen (szbuffer)], "[% d, % s]", (INT) ui32line, pszfilename); // The direction is from right to left, store the parameters on the right to the string variable on the left according to the format.

Sscanf (regbuf, "% 2 s", temp );
Sscanf (temp, "% 0x", & regaddr); // The direction is from left to right, and the parameters on the left are saved to the variables on the right according to the format.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.