C code implementation of field alignment and filling during file writing

Source: Internet
Author: User

C code implementation of field alignment and filling during file writing

I. Overview
In actual software development projects, you often encounter the problem of converting the field formats in some files. For example, if the length of some fields is fixed in a single file, if the length of the field is insufficient, then the insufficient digits are filled with some special characters (and may have requirements on the filling position ). The field format is fixed to facilitate automatic file processing, because many software processes files according to certain rules. This document describes how to fill fields.
This document requires that the field length in the generated file be fixed to 10. If the length of the written content is less than 10, the remaining part can be filled with spaces or 0. In addition, the program is required to control whether to fill and fill 0 or space on the left or right side of the written content.

II. C code implementation

/*************************************** * ******************************** All Rights Reserved (C) 2015, Zhou Zhaoxiong. ** File name: TestAlign. c * File ID: none * Content summary: handling of file content alignment and filling in the example * Other Instructions: none * Current version: V1.0 * Author: Zhou Zhaoxiong * completion date: 20150901 *************************************** * *******************************/# include
  
   
# Include
   
    
# Include
    
     
// Redefine the Data Type typedef signed int INT32; typedef signed char INT8; // function declaration void WriteFile (INT32 iAlignFlag, INT8 * pszTestStr); INT32 main (); /*************************************** * ******************************** function description: main function * input parameter: none * output parameter: none * return value: none * Other description: no * modified date version number modifier modified content * found * 20150901 V1.0 Zhou Zhaoxiong create *********************** ************ * **********************************/INT32 main () {INT32 iAlignFlag = 0; INT8 szTestStr [50] = {0}; printf (Please input alignment (1, 2, 3, 4) and test string ); scanf (AlignFlag = % d, TestStr = % s, & iAlignFlag, szTestStr); printf (input information: AlignFlag % d, test string % s, iAlignFlag, szTestStr ); writeFile (iAlignFlag, szTestStr); // call the Write File function return 0; // return 0 if the main function is successfully executed }/****************************** **************************************** * function description: based on alignment Type Write File * input parameter: iAlignFlag-alignflag-alignaling pszTestStr-test string * output parameter: none * return value: none * Other Description: Set the reference length to 10 bytes, if it is not reached, fill in space or 0 * modify the date version number. modify the content * required * 20150901 V1.0 Zhou Zhaoxiong to create ***************** **************************************** * ************/void WriteFile (INT32 iAlignFlag, INT8 * pszTestStr) {FILE * fp = NULL; INT8 szContent [50] = {0}; INT8 szFileName [50] = {0}; if (pszTestStr = NULL) // determines whether the input parameter is NULL {printf (WriteFile: input parameter (s) is NULL !); Return;} if (iAlignFlag = 1) // left alignflag. The missing bits are filled with spaces {_ snprintf (szContent, sizeof (szContent)-1, % 10 s, pszTestStr);} if (iAlignFlag = 2) // right alignment, fill spaces with spaces {_ snprintf (szContent, sizeof (szContent)-1, %-10 s, pszTestStr);} if (iAlignFlag = 3) // left alignflag. The missing bits are 0 {_ snprintf (szContent, sizeof (szContent)-1, %-s % 0 * d, pszTestStr, 10-strlen (pszTestStr), 0);} if (iAlignFlag = 4) // right alignflag, fill in 0 {_ snprintf (szConte Nt, sizeof (szContent)-1, % 0 * d % s, 10-strlen (pszTestStr), 0, pszTestStr);} // write the file content to strcpy (szFileName, D: \ Test \ TestFile.txt); // Note: The file directory is double. Do not write it as D: TestTestFile.txt fp = fopen (szFileName, w); if (fp! = NULL) {fputs (szContent, fp); fclose (fp); fp = NULL; printf (WriteFile: Write % s into file successfully !, SzContent);} else {printf (WriteFile: Write % s into file failed !, SzContent );}}
    
   
  

Iii. program description
1. The program uses iAlignFlag to indicate the filling mode, as follows:
(1) If the value of iAlignFlag is 1, a space is filled on the right side of the written content (that is, the left alignflag of the content, and spaces are filled for the missing digits ).
(2) If the value of iAlignFlag is 2, the space is filled on the left side of the written content (that is, the right alignflag of the content, and the space is supplemented by spaces if the content is not adequate ).
(3) If the value of iAlignFlag is 3, it indicates that 0 is filled on the right side of the written content (that is, the content is left aligned, and the number of digits below is 0 ).
(4) If the value of iAlignFlag is 4, it indicates that the left side of the written content is filled with 0 (that is, the right alignflag of the content, and the insufficient bit is supplemented with 0 ).
2. Pay special attention to the method of filling 0 or space on the left or right side of the program, as follows:
(1) left alignment, right fill space:
_ Snprintf (szContent, sizeof (szContent)-1,"% 10 s", PszTestStr );
(2) right alignment, left space fill:
_ Snprintf (szContent, sizeof (szContent)-1,"%-10 s", PszTestStr );
(3) left alignment, right fill 0:
_ Snprintf (szContent, sizeof (szContent)-1,"%-S % 0 * d", PszTestStr, 10-strlen (pszTestStr), 0 );
(4) right alignment, left fill 0:
_ Snprintf (szContent, sizeof (szContent)-1,"% 0 * d % s", 10-strlen (pszTestStr), 0, pszTestStr );

Iv. program output
The program output is as follows:




At the same time, view the content of the "testfile.txt" file under "D: test.pdf", and you can see that the written content is correct.

V. Summary
Although the Code of this program is not much, It is not complicated, but you should pay special attention to the handling methods of alignment and character filling. If you want to write programs from the beginning, it is not easy to find the correct solution. This also shows that many problems seem simple, but to solve them perfectly, we still need to have a solid foundation and practice.

 

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.