Thread: Help replacing words in a file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Help replacing words in a file

    I'm trying to write a program that will open an existing file supplied by the command line argument and then replace words with "We" or "we" by "I" and "a" or "A" by "The". When I run the program it reads the file, changes the word but re writes it on a new line with only the replaced words not the whole sentence. Would anyone be able to help me?

    Code:
     
    #include        <stdio.h>
    #include	<stdlib.h>
    #include        <unistd.h>
    #include        <fcntl.h>
    
    #define BUFFERSIZE      4096
    
     main(int ac, char *av[])
    {
            int     in_fd, n_chars;
            char    buf[BUFFERSIZE];
            char x;
                                            /* check args   */
            if ( ac != 2 ){
                    fprintf( stderr, "usage: %s source destination\n", *av);
                    exit(1);
            }
                                                    /* open files   */
    
            if ( (in_fd=open(av[1], O_RDWR)) == -1 )
            	printf("Cannot open ", av[1]);
    
    
    	while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 ){
    
    
    	for  (x=0; x<n_chars; x++)
    	{
    
    
    		if (buf[x] ==  'W') if (buf[x+1] == 'e') if (buf[x+2]=' ')
    			{
    				lseek(in_fd, 0, SEEK_CUR);
    				write(in_fd, "I", strlen("I"));
    			}
    		if (buf[x] == 'w') if (buf[x+1] == 'e') if (buf[x+2]=' ')
    			{
    				lseek(in_fd, 0, SEEK_CUR);
    				write(in_fd, "I", strlen("I"));
    			}
    		if (buf[x] == 'A') if (buf[x+1] == ' ')
    			{
    				lseek(in_fd, 0, SEEK_CUR);
    				write(in_fd, "The", strlen("The"));
    			}
    		if (buf[x] == 'a') if (buf[x+2] == ' ')
    			{
    				lseek(in_fd, 0, SEEK_CUR);
    				write(in_fd, "the", strlen("the"));
    			}	
                    write( in_fd, &buf[x], 1 );			
    	}
    
    	}
            if ( n_chars == -1 )
            	printf("Read error from ", av[1]);
            if ( close(in_fd) == -1)
                    printf("Error closing files","");
    
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if ( (in_fd=open(av[1], O_RDWR))
    Open two different files, one for input, one for output.
    You're NEVER going to do an in-file search/replace for words which are of different lengths.

    Here,
    Code:
    #include <stdio.h>
    int main ( ) {
      char buff[BUFSIZ];
      FILE *in, *out;
      in = fopen( "file-in.txt", "r" );
      out= fopen( "file-out.txt", "w" );
      while ( fgets( buff, sizeof buff, in ) != NULL ) {
        fputs ( buff, out );
      }
      fclose( in );
      fclose( out );
      return 0;
    }
    This code simply makes a copy of the file.
    Just before the fputs() call, add your code to perform all the substitutions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM