The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace words in file based on another file kinmak Shell Programming and Scripting 9 05-07-2008 06:06 AM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 01:15 PM
Looking for Words File OC Del Guy UNIX for Dummies Questions & Answers 9 04-12-2006 08:27 AM
search for words in file Agent_Orange Shell Programming and Scripting 4 10-25-2002 06:29 AM
Counting words in a file r0mulus Shell Programming and Scripting 1 10-24-2002 10:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-06-2006
adam85 adam85 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 1
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.


Code:

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




}

  #2 (permalink)  
Old 04-07-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
Welcome to the Forum !!!
Quote:
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.
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
Quote:
write(in_fd, "I", strlen("I"));
try the following code,
not well tested, can be optimized, would server only as an example or starter;modify input/output file accordingly


Code:
# 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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0