![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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","");
}
|
|
||||
|
Welcome to the Forum !!! Quote:
In your code where have you, instructed to write the whole sentence? Only the replacement is effected as follows Quote:
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|