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? I also have to use open and read not fopen for example.


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

#define BUFFERSIZE      4096

 main(int ac, char *av[])
{
        int     in_fd, n_chars;
        char    buf[BUFFERSIZE];
        char x;

        if ( ac != 2 ){
                fprintf( stderr, "usage: %s source destination\n", *av);
                exit(1);
        }
                                        

        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","");




}


Welcome to the Forum !!! :slight_smile: :slight_smile: :slight_smile:

Yes it would rewrite on the newline and that too with only the replaced words not the whole sentence.
In your code where have you, instructed to write the whole sentence? Only the replacement is effected as follows

try the following code,
not well tested, can be optimized, would server only as an example or starter;modify input/output file accordingly

# include<stdio.h>

int main()
{
 FILE *fpr, *fpw;
 int ch1, ch2, ch3;

 if( ((fpr=fopen("src", "r")) == NULL) || ((fpw=fopen("dest", "w")) == NULL ))
 {
    exit(1);
 }
  while( (ch1=fgetc(fpr)) != EOF )
  {
     if( (ch2=fgetc(fpr)) == EOF )
     {
        break;
     }
     if( (ch3=fgetc(fpr)) == EOF )
     {
        break;
     }
     if( (ch1=='W' && ch2=='e' && ch3==' ') || (ch1=='w' && ch2=='e' && ch3==' ') )
     {
        fprintf(fpw, "%s", "I ");
        continue;
     }
   else if( (ch1=='a' && ch2==' ') || (ch1=='A' && ch2==' ') )
     {
        fprintf(fpw, "%s", "The ");
        ungetc(ch3, fpr);
        continue;
     }
     else
     {
        fprintf(fpw, "%c",ch1);
        ungetc(ch3, fpr);
        ungetc(ch2, fpr);
     }
  }
  if(ch1 != EOF)
     fprintf(fpw, "%c",ch1);
  if(ch2 != EOF)
     fprintf(fpw, "%c",ch2);
  if(ch3 != EOF)
     fprintf(fpw, "%c",ch3);
  fclose(fpr);
  fclose(fpw);
}

>cat src
we source of power
We ultimate source of power
of all that a possible
neutral of all A neurons

>cat dest
I source of power
I ultimate source of power
of all that The possible
neutral of all The neurons