C Code implementation for field alignment and padding when writing files

Source: Internet
Author: User

I. Overview
In a real-world software development project, you often encounter problems that require you to convert the format of a field in some files. For example, in a generated single file, some fields are required to be fixed in length, and if the length of the field is currently insufficient, the insufficient number of digits is populated with some special characters (and may be required for the location of the fill). The field format is required to facilitate the automatic processing of files, because many software is in accordance with certain rules of the processing of files. This article has an example of how to populate a field.
This article requires that the field length in the makefile be fixed to 10, and if the content written is less than 10, the insufficient portion can be filled with spaces or zeros. In addition, the program is required to control whether the left or right padding and padding is 0 or a space on the write content.

Second, C code implementation

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: TESTALIGN.C* File ID: None* Content Summary: Sample handling of File content alignment and padding* Other instructions: None* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20150901***********************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>Redefine 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 parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* -------------------------------------------------------------------* 20150901 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {INT32 ialignflag = 0;INT8 szteststr[50] = {0};printf ("Please enter alignment (1, 2, 3, 4) and test string \ n");scanf ("alignflag=%d, teststr=%s", &ialignflag, szteststr);printf ("Input information: alignment%d, test string%s\n", Ialignflag, szteststr);WriteFile (Ialignflag, SZTESTSTR); Calling the Write file functionreturn 0; Main function execution successfully returned 0}/*********************************************************************** Function Description: Write the file according to the alignment method* Input parameters: ialignflag-Alignmentpszteststr-Test String* Output parameters: None* return value: None* Other instructions: Set a base length of 10 bytes, not up to the fill space or 0* Modified Date version number modify the content of the person* -------------------------------------------------------------------* 20150901 V1.0 Zhou Zhaoxiong created***********************************************************************/void WriteFile (INT32 ialignflag, INT8 *pszteststr) {FILE *fp = NULL;INT8 szcontent[50] = {0};INT8 szfilename[50] = {0};if (pszteststr = = NULL)//Determine if the input parameter is empty    {printf ("Writefile:input parameter (s) is null!");return;    }if (Ialignflag = = 1)//left justified, insufficient bit fill space    {_snprintf (szcontent, sizeof (szcontent)-1, "%10s", pszteststr);    }if (Ialignflag = = 2)//right-justified, insufficient bit fill space    {_snprintf (szcontent, sizeof (szcontent)-1, "%-10s", pszteststr);    }if (Ialignflag = = 3)//left justified, insufficient bit complement 0    {_snprintf (szcontent, sizeof (szcontent)-1, "%-s%0*d", Pszteststr, 10-strlen (PSZTESTSTR), 0);    }if (Ialignflag = = 4)//right alignment, insufficient bit complement 0    {_snprintf (szcontent, sizeof (szcontent)-1, "%0*d%s", 10-strlen (PSZTESTSTR), 0, pszteststr);    }//write the contents of the filestrcpy (szFileName, "d:\\test\\testfile.txt");//Note: The file directory is dual \, do not write "D:\Test\TestFile.txt"fp = fopen (szFileName, "w");if (fp! = NULL)    {fputs (szcontent, FP);fclose (FP);fp = NULL;printf ("Writefile:write%s into file successfully!\n", szcontent);    }Else    {printf ("Writefile:write%s into file failed!\n", szcontent);    }}

iii. Description of the procedure
1. This procedure uses the Ialignflag to indicate the filling method, the specific following:
(1) If Ialignflag is 1, a space is filled to the right of the content being written (that is, the content is left-justified, and the insufficient bits fill the space).
(2) If Ialignflag is 2, a space is filled to the left of the write content (that is, the content is right-aligned and insufficient bits fill the space).
(3) If Ialignflag is 3, it means padding 0 on the right side of the write content (that is, the content is left-aligned and insufficient bit-fill 0).
(4) If Ialignflag is 4, it means padding 0 on the left side of the write content (that is, the content is right-aligned and insufficient bits complement 0).
2. Special attention should be paid to the method of filling 0 or spaces on the left or right side of the program, as follows:
(1) left-justified, right-fill space:
_snprintf (szcontent, sizeof (szcontent)-1, "%10s", pszteststr);
(2) Right-aligned, left-fill space:
_snprintf (szcontent, sizeof (szcontent)-1, "%-10s", pszteststr);
(3) left-justified, right-fill 0:
_snprintf (szcontent, sizeof (szcontent)-1, "%-s%0*d", Pszteststr, 10-strlen (PSZTESTSTR), 0);
(4) Right alignment, left 0:
_snprintf (szcontent, sizeof (szcontent)-1, "%0*d%s", 10-strlen (PSZTESTSTR), 0, PSZTESTSTR);

iv. output of the program
The program output is as follows:




Also, look at the contents of the "TestFile.txt" file under "D:\Test", and you can see that the write content is correct.

v. Summary
Although this program code is not many, is not complex, but we should pay special attention to the alignment and character fill processing methods. It's not so easy to find the right approach if you're getting people to write programs from scratch. This also shows that a lot of problems seem simple, but to solve it perfectly, we also need to have a solid foundation, but also need us to practice.

Welcome to the attention and support of my new book "C Programmer from the campus to the workplace."

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Code implementation for field alignment and padding when writing files

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.