john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file replace keyword strstr

/* john pfeiffer 2010-06 replace a string in a file with another

complicated by my fear that a char may not always be the same size (8 bytes versus ?)
how to turn a pointer location in a string into a 
  number of characters

multiple copies of the keyword in a line?
TODO linux sed format 's/{old value}/{new value}/'

*/

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

int main(int argc, char* argv[])
{
    int i;
    char c;
    FILE* fptr;
    char filename[256];
    char linebuffer[1024]; 
    char keyword[256];
    char replacement[256];

    int linecounter = 0;
    int  keywordcounter=0;
    char* keywordlocation = NULL;
    char* bufferlocation = NULL;

    if( argc != 4 )
    {   printf("\nwrong parameters, e.g. %s infile.txt keyword replacement\n" , argv[0] );
        exit( 1 );
    }

    strcpy( filename , argv[1] );
    strcpy( keyword , argv[2] );
    strcpy( replacement , argv[3] );

    fptr = fopen( filename , "r" );    
    if( fptr == NULL)
    {   printf( "error opening input file!" );
        exit( 1 );      
    }

    while( ! feof( fptr )  )                            
    {   
        linecounter++;  
        fgets( linebuffer , 1024 , fptr);

        keywordlocation = strstr( linebuffer , keyword );

        if( keywordlocation != NULL )
        {   
            printf( "%d   " , linecounter );

            bufferlocation = linebuffer;            
            while( *bufferlocation != '\0' )
            {
                printf( "%c" , *bufferlocation );
                bufferlocation = bufferlocation + sizeof( c ) ;

                if( bufferlocation == keywordlocation)
                {

                    for( i = 0 ; i < strlen( keyword ) ; i++ )
                    {
                        /* printf("%c" , *keywordlocation ); */
                        if( i < strlen( replacement ) )
                        {   fputc( replacement[i] , stdout);   
                        }

                        keywordlocation = keywordlocation + sizeof( c ) ;
                        bufferlocation = bufferlocation + sizeof( c ) ;
                    }
                }


            } /* end while not end of linebuffer string */

            keywordcounter++;
        }

        if( keywordlocation == NULL && !feof( fptr ) )
        {
            fputs( linebuffer , stdout );
        }

    } /* end of file */

    fclose( fptr );

/*    
    printf("\nlines:%d \n" , linecounter );
    printf("lines with keywords:%d" , keywordcounter );
*/  
    return 0;
}/*end of main*/


/* return a char pointer back to the modified char array ? 
   WHAT IF THE REPLACEMENT IS LARGER THAN THE ORIGINAL!
   WHAT ABOUT MULTIPLE KEYWORD MATCHES ON THE SAME LINE?

char* substring_replace( char* buffer , int buffersize , char* keyword , int keyword size , char* replacement , int replacementsize )
{



}end of substring_replace*/

  • « array pointer arithmetic strstr
  • Trac wiki install plugins »

Published

Jun 12, 2010

Category

c

~267 words

Tags

  • c 95
  • file 92
  • keyword 2
  • replace 7
  • strstr 2