• removeLineEndSpace


     1 /******************************************************************************
     2  *                          removeLineEndSpace
     3  *  声明:
     4  *      该程序是之前alignBySpace的逆向补充程序,去掉行尾的空格。
     5  *      
     6  *                                   2015-8-11 阵雨 深圳 南山平山村 曾剑锋
     7  *****************************************************************************/
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <stdlib.h>
    11 
    12 int main ( int argc, char ** argv ) {
    13 
    14     int    ret                      = 0;
    15     char  *tempFile                 = "generateFile.txt";
    16     char  *readBuffer               = NULL;
    17     size_t bufferSize               = 512;
    18     int    readBufferIndex          = 0;
    19 
    20     if (  argc != 2  ) {
    21         printf ( "USAGE:
    " );
    22         printf ( "   removeLineEndSpace <file>.
    " );
    23         return -1;
    24     }
    25 
    26     FILE *readfile  = fopen ( argv[1], "r" );                         // only read
    27     FILE *writefile = fopen ( tempFile, "w" );                        // only write
    28 
    29     /**
    30      * man datasheet:
    31      *    If *lineptr is NULL, then getline() will allocate a buffer for storing the line, 
    32      *    which should be freed by the user program. (In this case, the value in *n is ignored.)
    33      */
    34     while ( ( ret = getline( &readBuffer, &bufferSize, readfile ) ) != -1 ) {
    35 
    36         readBufferIndex  = ret - 1; // index for real position
    37 
    38         while ( ( readBuffer [ readBufferIndex ] == ' ' ) ||          // del ' ', '	', '
    ' character
    39                 ( readBuffer [ readBufferIndex ] == '
    ' ) || ( readBuffer [ readBufferIndex ] == '	' ) )  
    40             readBuffer [ readBufferIndex-- ] = '';
    41         
    42         readBuffer [ readBufferIndex + 1 ] = '
    ';
    43 
    44         fputs ( readBuffer, writefile );                              // write to file 
    45 
    46         bzero ( readBuffer,  ret );                                   // clean buffer
    47     }
    48 
    49     free ( readBuffer );                                              // referrence getline()
    50 
    51     fclose ( readfile );
    52     fclose ( writefile );
    53 
    54     remove ( argv[1] );                                               // delete source file
    55     rename ( tempFile, argv[1] );                                     // tempfile rename to source file
    56 }
  • 相关阅读:
    详细的解说public,protected,Default和private的权限问题
    谈谈java中静态变量与静态方法在有继承关系的两个类中调用
    谈谈java中成员变量与成员方法继承的问题
    谈谈嵌套for循环的理解
    JAVA基础细谈
    Css的使用细谈
    Hibernate映射
    hibernate基本
    struts2
    s:form标签
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/4719951.html
Copyright © 2020-2023  润新知