空格填充器(alignBySpace)

时间:2024-09-04 10:04:20
 /*******************************************************************
* 空格填充器(alignBySpace)
* 声明:
* 1. 软件主要是在不改变文本文件内容情况下,自动填充空格一定数量的
* 空格,达到空格右对齐的功能;
* 2. 本软件主要是节省个人的代码跟踪文档打空格的时间;
*
* 2015-8-3 晴 深圳 南山平山村 曾剑锋
******************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> int main ( int argc, char ** argv ) { int ret = ;
int column = ;
char *tempFile = "generateFile.txt";
char *readBuffer = NULL;
size_t bufferSize = ;
int readBufferIndex = ;
int writeBufferIndex = ;
char writeBuffer[bufferSize]; if ( ( argc < ) || ( argc > ) ) {
printf ( "USAGE:\n" );
printf ( " alignBySpace <file> [column].\n" );
return -;
} if ( argc == ) {
printf ( "\nUse default columns: 80. \n\n" );
column = ;
} else
column = atoi ( argv[] ); FILE *readfile = fopen ( argv[], "r" ); // only read
FILE *writefile = fopen ( tempFile, "w" ); // only write /**
* man datasheet:
* If *lineptr is NULL, then getline() will allocate a buffer for storing the line,
* which should be freed by the user program. (In this case, the value in *n is ignored.)
*/
while ( ( ret = getline( &readBuffer, &bufferSize, readfile ) ) != - ) { readBufferIndex = ;
writeBufferIndex = ; while ( readBufferIndex < ret ) {
if ( readBuffer [ readBufferIndex ] != '\t' ) // kick out '\t'
writeBuffer [ writeBufferIndex++ ] = readBuffer [ readBufferIndex++ ];
else {
memset ( writeBuffer + writeBufferIndex, ' ', ); // every tab key was 4 space
writeBufferIndex += ;
readBufferIndex++;
}
} writeBufferIndex--; // back to real index if ( ( column - writeBufferIndex ) > ) { // may be real column longer than we set
// '\n' at end of a line, need to kick it out
memset ( writeBuffer + writeBufferIndex, ' ', column - writeBufferIndex );
writeBuffer [ column ] = '\n'; // for end of a line
writeBuffer [ column + ] = '\0'; // for end of a string
} else
writeBuffer [ writeBufferIndex + ] = '\0'; // make sure end of a string fputs ( writeBuffer, writefile ); // write to file bzero ( readBuffer, bufferSize ); // clean buffer
bzero ( writeBuffer, bufferSize ); } free ( readBuffer ); // referrence getline() fclose ( readfile );
fclose ( writefile ); remove ( argv[] ); // delete source file
rename ( tempFile, argv[] ); // tempfile rename to source file
}