john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

add ascii character after ascii char

/* John Pfeiffer, 15jan09 add a character after a given character, 
parameters are ascii numbers, in a file

needs to open a file
needs to "find" the first char
needs to write to a second file
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_PARAMETERS 4  /* =1 b/c the 1st parameter is the program name */
#define MAX_PARAMETER_SIZE 512     /* the max size of a given parameter */


void program_usage_help( char safe_argv[NUMBER_OF_PARAMETERS][MAX_PARAMETER_SIZE] )
{   printf("%s 97 45 filename.txt",safe_argv[0]); 
}/* end program_usage_help adds a '-' after ever 'a' */

void safe_parameters_array( int argc, char* argv[], char safe_argv[NUMBER_OF_PARAMETERS][MAX_PARAMETER_SIZE] )
{    int i=0, k=0;

    for(i=0; i<argc; i++)
    {                                 /* params are null terminated strings */
        while( (k < MAX_PARAMETER_SIZE) && (argv[i][k] != '\0') )  
        {                                   
            safe_argv[i][k] = argv[i][k];
            k++;
        }

        safe_argv[i][k] = '\0';       /* we must always null terminate a string! */     
        k=0;
        printf("safe_argv[%d]=%s\n",i,safe_argv[i]);  
     }/* end for loop */        
}/* end safe_parameters_array function */

FILE* open_outfile(char* argv[], FILE* ofp)
{
    char ofname[40];

    strcpy(ofname,argv[3]);     /* 3rd param is input file */
    strcat(ofname,".txt");

    ofp = fopen( ofname,"w" );
    if( ofp == NULL )
    {   printf("outfile open error!");
        exit(1);
    }else{    printf("creating %s\n",ofname);   }

    return ofp;
}/* end open_outfile func */

/* MAIN ------------------------------------------------------- */
int main(int argc, char* argv[])
{
    char safe_argv[NUMBER_OF_PARAMETERS][MAX_PARAMETER_SIZE];
    int i=0;
    char c;
    int char_to_find=0;
    int char_to_add=0;
    FILE* ifp;
    FILE* ofp;

    if( (argc > NUMBER_OF_PARAMETERS) || (argc < NUMBER_OF_PARAMETERS) )
    {   printf("Wrong number of parameters, should be %d parameters, e.g. 97 45 filename.txt",(NUMBER_OF_PARAMETERS-1));
        exit(1);                    /* quit program with error */
    }

    safe_parameters_array( argc, argv, safe_argv );

    char_to_find = atoi(safe_argv[1]);
    char_to_add = atoi(safe_argv[2]);

    if( (char_to_find > 128) || (char_to_add > 128) || (char_to_find < 0) || (char_to_add < 0) )
    {
        printf("\nerror, input parameters should be two ascii integers between 0 and 128\n");        
        exit(1);
    }

    /* ##################### two ascii parameters are ok ############### */

    ifp = fopen(argv[3],"r");
    ofp = open_outfile(argv, ofp);

    if( ifp == NULL || ofp == NULL )
    {   printf("error opening file\n");    
        exit(1);
    }

    /* ##################### file to modify is ok ############### */



    c = getc(ifp);
    while( c!= EOF)
    {
        if( ((int)c) != char_to_find  )
        {
            putc(c,ofp); 
        }
        else
        {
            putc(c,ofp);
            putc( (char)char_to_add, ofp );            
            i++;
        }
        c = getc(ifp);

    }

    fclose(ifp);
    fclose(ofp);

    printf("%d characters added",i);

    return 0;
}/* end main */

  • « date time calendar
  • wndclass with comments »

Published

Jan 15, 2009

Category

c

~283 words

Tags

  • add 9
  • after 1
  • ascii 4
  • c 95
  • char 3
  • character 2