zoukankan      html  css  js  c++  java
  • 空格填充器(alignBySpace)

     1 /*******************************************************************
     2  *                     空格填充器(alignBySpace)
     3  * 声明:
     4  *  1. 软件主要是在不改变文本文件内容情况下,自动填充空格一定数量的
     5  *      空格,达到空格右对齐的功能;
     6  *  2. 本软件主要是节省个人的代码跟踪文档打空格的时间;
     7  *
     8  *                             2015-8-3 晴 深圳 南山平山村 曾剑锋
     9  ******************************************************************/
    10 #include <stdio.h>
    11 #include <string.h>
    12 #include <stdlib.h>
    13 
    14 int main ( int argc, char ** argv ) {
    15 
    16     int    ret                      = 0;
    17     int    column                   = 0;
    18     char  *tempFile                 = "generateFile.txt";
    19     char  *readBuffer               = NULL;
    20     size_t bufferSize               = 512;
    21     int    readBufferIndex          = 0;
    22     int    writeBufferIndex         = 0;
    23     char   writeBuffer[bufferSize];
    24 
    25     if ( ( argc < 2 ) || ( argc > 3 ) ) {
    26         printf ( "USAGE:
    " );
    27         printf ( "    alignBySpace <file> [column].
    " );
    28         return -1;
    29     }
    30 
    31     if ( argc == 2 ) {
    32         printf ( "
    Use default columns: 80. 
    
    " );
    33         column = 80;
    34     } else 
    35         column = atoi ( argv[2] );
    36 
    37     FILE *readfile  = fopen ( argv[1], "r" );                         // only read
    38     FILE *writefile = fopen ( tempFile, "w" );                        // only write
    39 
    40     /**
    41      * man datasheet:
    42      *    If *lineptr is NULL, then getline() will allocate a buffer for storing the line, 
    43      *    which should be freed by the user program. (In this case, the value in *n is ignored.)
    44      */
    45     while ( ( ret = getline( &readBuffer, &bufferSize, readfile ) ) != -1 ) {
    46 
    47         readBufferIndex  = 0;
    48         writeBufferIndex = 0;
    49 
    50         while ( readBufferIndex < ret ) {
    51             if ( readBuffer [ readBufferIndex ] != '	' )             // kick out '	'
    52                 writeBuffer [ writeBufferIndex++ ] = readBuffer [ readBufferIndex++ ];
    53             else {
    54                 memset ( writeBuffer + writeBufferIndex, ' ', 4 );    // every tab key was 4 space
    55                 writeBufferIndex += 4;    
    56                 readBufferIndex++;
    57             }
    58         }
    59 
    60         writeBufferIndex--;                                           // back to real index
    61 
    62         if ( ( column - writeBufferIndex ) > 0 ) {                    // may be real column longer than we set
    63             // '
    ' at end of a line, need to kick it out
    64             memset ( writeBuffer + writeBufferIndex, ' ', column - writeBufferIndex );
    65             writeBuffer [ column ] = '
    ';                            // for end of a line 
    66             writeBuffer [ column + 1 ] = '';                        // for end of a string
    67         } else 
    68             writeBuffer [ writeBufferIndex + 1 ] = '';              // make sure end of a string
    69 
    70         fputs ( writeBuffer, writefile );                             // write to file 
    71 
    72         bzero ( readBuffer,  bufferSize );                            // clean buffer
    73         bzero ( writeBuffer, bufferSize );
    74 
    75     }
    76 
    77     free ( readBuffer );                                              // referrence getline()
    78 
    79     fclose ( readfile );
    80     fclose ( writefile );
    81 
    82     remove ( argv[1] );                                               // delete source file
    83     rename ( tempFile, argv[1] );                                     // tempfile rename to source file
    84 }
  • 相关阅读:
    iOS开发 数据缓存-数据库
    我是程序猿,我自豪,我骄傲!嗷嗷嗷!
    【Golang】练习-复制文件
    【Golang】练习-读取目录下的文件并按时间进行排序
    【Golang】练习 tailf 简单模拟
    【Golang】基础-切片 for 循环删除元素
    【Golang】基础-操作 csv 文件
    【Golang】Godoc 或者第三方 plugins 插件介绍
    【Golang】类型转换之 cast 包
    【Golang】解决 go get下载包慢或者失败的问题
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/4697348.html
Copyright © 2011-2022 走看看