Sponsored Content
Full Discussion: Replacing words in a file
Top Forums Programming Replacing words in a file Post 302070711 by adam85 on Thursday 6th of April 2006 06:22:27 PM
Old 04-06-2006
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","");




}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed replacing words with abbreviations

I really hate to do this, but I am completely stumped. I have to create a sed script that will change the abbreviations in a file to the full word. I really just have no idea where to start. All I want is a starting point as well no actual complete answer. Thank you for your time in advance. ... (4 Replies)
Discussion started by: mauler123
4 Replies

2. Shell Programming and Scripting

Replacing words in a fast way

Hi, I have a file that looks like this: br0 br0 br1 br10 br11 br12 br13 br14 br15 br15 br2 br2 br3 br4 br5 br6 br7 (5 Replies)
Discussion started by: phil_heath
5 Replies

3. Shell Programming and Scripting

Replacing words

Hi, I have am using a file that contains names that I want to replace. Basically file 1 looks like this jack joe james john I have another file (file 2) that looks like this jack 2345 joe 6848 james 3342 john 3432 Basically I want to replace column1 from file1... (4 Replies)
Discussion started by: kylle345
4 Replies

4. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

5. Shell Programming and Scripting

Replacing words in file

Hello All Probably this is very simple for you but I cant figure it out I have to replace "No Header" with "Output Field Names" I/P file <ATTRIBUTE NAME ="Header Options" VALUE ="No Header"/> O/P needed <ATTRIBUTE NAME ="Header Options" VALUE = "Output Field Names"> (4 Replies)
Discussion started by: Pratik4891
4 Replies

6. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

7. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

8. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

9. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

10. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies
SENDFILE(2)						     Linux Programmer's Manual						       SENDFILE(2)

NAME
sendfile - transfer data between file descriptors SYNOPSIS
#include <sys/sendfile.h> ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); DESCRIPTION
sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more effi- cient than the combination of read(2) and write(2), which would require transferring data to and from user space. in_fd should be a file descriptor opened for reading and out_fd should be a descriptor opened for writing. If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start reading data from in_fd. When sendfile() returns, this variable will be set to the offset of the byte following the last byte that was read. If offset is not NULL, then sendfile() does not modify the current file offset of in_fd; otherwise the current file offset is adjusted to reflect the number of bytes read from in_fd. If offset is NULL, then data will be read from in_fd starting at the current file offset, and the file offset will be updated by the call. count is the number of bytes to copy between the file descriptors. The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket). In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file. If it is a regular file, then send- file() changes the file offset appropriately. RETURN VALUE
If the transfer was successful, the number of bytes written to out_fd is returned. On error, -1 is returned, and errno is set appropri- ately. ERRORS
EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write would block. EBADF The input file was not opened for reading or the output file was not opened for writing. EFAULT Bad address. EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd. EIO Unspecified error while reading from in_fd. ENOMEM Insufficient memory to read from in_fd. VERSIONS
sendfile() is a new feature in Linux 2.2. The include file <sys/sendfile.h> is present since glibc 2.1. CONFORMING TO
Not specified in POSIX.1-2001, or other standards. Other UNIX systems implement sendfile() with different semantics and prototypes. It should not be used in portable programs. NOTES
If you plan to use sendfile() for sending files to a TCP socket, but need to send some header data in front of the file contents, you will find it useful to employ the TCP_CORK option, described in tcp(7), to minimize the number of packets and to tune performance. In Linux 2.4 and earlier, out_fd could also refer to a regular file, and sendfile() changed the current offset of that file. The original Linux sendfile() system call was not designed to handle large file offsets. Consequently, Linux 2.4 added sendfile64(), with a wider type for the offset argument. The glibc sendfile() wrapper function transparently deals with the kernel differences. Applications may wish to fall back to read(2)/write(2) in the case where sendfile() fails with EINVAL or ENOSYS. The Linux-specific splice(2) call supports transferring data between arbitrary files (e.g., a pair of sockets). SEE ALSO
mmap(2), open(2), socket(2), splice(2) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2011-09-14 SENDFILE(2)
All times are GMT -4. The time now is 12:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy