Question

****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT...

****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc

*****You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions)

You will write a C program and the way to use it on command line is as follows:

./myprogram file a t   newfile

where both file and newfile are text files, “a” is a single character ( of your choice), and “t” is a single character( of your choice). For example, you could run this program as

./myprogram file b k newfile

What your program does is to create a new file (i.e, newfile) such that the content of newfile is the same as the content of file but every occurrence of the character “a” in file is now replaced by character “t” in newfile.

For example, suppose the content of “file” is

apple bee elephant abort

If I run

./myprogram file a t   newfile

Then, the content of the “newfile” should be

tpple bee elephtnt tbort

You could see every occurrence of “a” in file is now “t” in newfile.

If I run

./myprogram file b   x   newfile

Then, the content of the “newfile” should be

apple xee elephant axort

You could see every occurrence of “b” in file is now “x” in newfile.

Write your program using system i/o functions.

Homework Answers

Answer #1

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main ( int argc, char **argv )
{
   char infile[32] = "";
   char outfile[32] = "";
   char inbuf[256] = "";
      
   strcpy(infile, argv[1]);
   strcpy(outfile, argv[4]);

   char c = argv[2][0];
   char r = argv[3][0];

   printf(" c= <%c> r = <%c> infile = <%s> outfile = <%s> \n", c, r, infile, outfile );

   int fd1 = open( infile, O_RDONLY | O_CREAT);
   int fd2 = open( outfile, O_CREAT | O_RDWR , S_IRWXU );

   int size = lseek(fd1, 0, SEEK_END);
   lseek(fd1, 0, SEEK_SET);
  
   int sr = read(fd1, inbuf, size);

   for(int i = 0; i<= size; i++)
   {
       if(inbuf[i] == c)
           inbuf[i] = r;
   }

   if( lseek(fd2, 0, SEEK_SET) == -1)
       perror("lseek error");

   int sw = write(fd2, inbuf, size);

   if (sw == -1)
       perror("write error ");

   close(fd1); close(fd1);
  
   return 0;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Write a program that is required to use nested loops to generate a triangle as shown in the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code which will read in strings from nemo.txt, then call Lab08(string) on each line of nemo.txt. Your job is to write Lab08.m. The Lab08 function will take in a string as input and will return an integer n as output. The value of n will be the nth word in the input string, where n is the location of the substring 'Nemo'. Please note that...
Review the Robatelli's Pizzeria Case Study. Develop another internal controls system, but this time, in the...
Review the Robatelli's Pizzeria Case Study. Develop another internal controls system, but this time, in the purchases and fixed assets business areas. Prepare a 12- to 16-slide presentation describing the purchases and fixed assets business areas. Be sure to incorporate speaker notes as well as appropriate visuals, graphics, fonts, etc. Include any associated risk in these areas. Describe specific internal controls that include authorization of transactions, segregation of duties, adequate records and documentation, security of assets, and independent checks and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT