SlideShare a Scribd company logo
1 of 3
Download to read offline
copyReverse.c code please do not change anything in the code besides the (four slashes ex.////)
/* copyReverse -- copyReverse namel to name2 */ #include #include #include #include
#include // !!! DO NOT CHNAGE BUFSIZE #define BUFSIZE 100 /* size of chunk to
be read */ #define PERM 0644 /* file permission for new file */ // sawpping two char
variables pointed by a and b // e.g., if you want to swap two variables, buf[i] and buf[j] //
you should call "swap(&buffer[i], &buffer[j])" by passing references (i.e., addresses) void
swap(char* a, char* b) { char t = *a; *a = *b; *b = t; } /* copyReverse namel to name2
*/ int copyReverse( const char *namel, const char *name2) { int infile, outfile; ssize_t
nread; char buffer[BUFSIZE]; if( ( infile = open(namel, O_RDONLY) ) == -1) return (-
1); if((outfile = open(name2,O_WRONLY|O_CREAT|O_TRUNC,PERM))==-1) { close
(infile); return (-2); } // find out the size of file (name1) by using lseek call ////int fSize =
lseek( ... ); // find out the number of blocks you need to read-reverse-write ////int numBlocks =
... // find out the size of partial block at the end ////int sizePartialBlock = ... // find the location
of RWP to be moved if sizePartialBlock > 0 int begBlockIdx = fSize - sizePartialBlock; // if
sizePartialBlock > 0, then you need to read it and reverse and write // you have to use lseek(),
read(), swap(), and write() calls to do this //*** it is recommended to use SEEK_SET flag in
lseek() call // //if(sizePartialBlock > 0) { //// //// //// //// //// } // Now, you need to reverse the
remaining blocks from the end //(except for the PartialBlcok) // int cnt = 0; // to keep track of
the number of blocks read/reversed while(cnt < numBlocks) { begBlockIdx = fSize -
sizePartialBlock - ((cnt+1) * BUFSIZE); // move the read-writer pointer to point to the
beginning of the // block to be read! You need to use lseek() //// //// // read one block bytes
(100 bytes) into buffer //from the (numBlocks-cnt)-th block from the beginning of name1 file
//// nread = read( ... ); //// // reverse bytes in buffer - you should use swap function declared
above //for swapping two char variables. Note nread contains the actual number //of bytes read.
//You may use additional extra array in this process - but there will //be some penalty if you do.
Try to do it within buffer[] //without using additional memory! //// //// // write reversed buffer
into name2 file - make sure to write only nread bytes //// //// cnt++; // increment the number of
blocks read/reversed/written } // end of while loop! close(infile); close (outfile); return(0);
// normal return } int main(int argc, char**argv) { if(argc!=3) { printf("Usage: copyReverse
file1 file2 n"); exit(-1); } int retcode=0; retcode = copyReverse(argv[1], argv[2]); }the inputs
and outputs below as examples input1.txt It was many and many a year ago, In a kingdom by
the sea, That a maiden there lived whom you may know By the name of ANNABEL LEE;--
And this maiden she lived with no other thought Than to love and be loved by me. She was a
child and I was a child, In this kingdom by the sea, But we loved with a love that was more
than love-- I and my Annabel Lee-- With a love that the winged seraphs of heaven Coveted
her and me. And this was the reason that, long ago, In this kingdom by the sea, A wind blew
out of a cloud by night Chilling my Annabel Lee; So that her high-born kinsman came And
bore her away from me, To shut her up in a sepulchre In this kingdom by the sea. The angels,
not half so happy in Heaven, Went envying her and me:-- Yes! that was the reason (as all men
know, In this kingdom by the sea) That the wind came out of a cloud, chilling And killing my
Annabel Lee. But our love it was stronger by far than the love Of those who were older than
we-- Of many far wiser than we- And neither the angels in Heaven above, Nor the demons
down under the sea, Can ever dissever my soul from the soul Of the beautiful Annabel Lee:--
For the moon never beams without bringing me dreams Of the beautiful Annabel Lee; And the
stars never rise but I see the bright eyes Of the beautiful Annabel Lee; And so, all the night-
tide, I lie down by the side Of my darling, my darling, my life and my bride, In her sepulchre
there by the sea-- In her tomb by the side of the sea. input2.txt From childhood's hour I have
not been output1.txt .aes eht fo edis eht yb bmot reh nI --aes eht yb ereht erhclupes reh nI
,edirb ym dna efil ym ,gnilrad ym ,gnilrad ym fO edis eht yb nwod eil I ,edit-thgin eht lla ,os
dnA ;eeL lebannA lufituaeb eht fO seye thgirb eht ees I tub esir reven srats eht dnA ;eeL
lebannA lufituaeb eht fO smaerd em gnignirb tuohtiw smaeb reven noom eht roF --:eeL
lebannA lufituaeb eht fO luos eht morf luos ym revessid reve naC ,aes eht rednu nwod snomed
eht roN ,evoba nevaeH ni slegna eht rehtien dnA -ew naht resiw raf ynam fO --ew naht redlo
erew ohw esoht fO evol eht naht raf yb regnorts saw ti evol ruo tuB .eeL lebannA ym gnillik
dnA gnillihc ,duolc a fo tuo emac dniw eht tahT )aes eht yb modgnik siht nI ,wonk nem lla
sa( nosaer eht saw taht !seY --:em dna reh gniyvne tneW ,nevaeH ni yppah os flah ton ,slegna
ehT .aes eht yb modgnik siht nI erhclupes a ni pu reh tuhs oT ,em morf yawa reh erob dnA
emac namsnik nrob-hgih reh taht oS ;eeL lebannA ym gnillihC thgin yb duolc a fo tuo welb
dniw A ,aes eht yb modgnik siht nI ,oga gnol ,taht nosaer eht saw siht dnA .em dna reh
detevoC nevaeh fo shpares degniw eht taht evol a htiW --eeL lebannA ym dna I --evol naht
erom saw taht evol a htiw devol ew tuB ,aes eht yb modgnik siht nI ,dlihc a saw I dna dlihc a
saw ehS .em yb devol eb dna evol ot nahT thguoht rehto on htiw devil ehs nediam siht dnA --
;EEL LEBANNA fo eman eht yB wonk yam uoy mohw devil ereht nediam a tahT ,aes eht yb
modgnik a nI ,oga raey a ynam dna ynam saw tI output2.txt neeb ton evah I ruoh s'doohdlihc
morF
(3) You are allowed to use only the system calls covered in the class (e.g., read 0 , write ), open
0 , Iseek (, close()). You can't use library calls such as fread 0 , fopen 0 , fwrite 0 , etc. If you
don't follow this requirement, you will get significant penalty!! (4) Do not change other parts of
your program except for the parts beginning with four slashes - there are multiple parts.
Assignment Description: Given the template code, "copyReverse. c ", fill up the marked portion
so that your program is performing the following tasks: - Read from input file whose name is
pointed by argv[1]. - Write its reversed file contents into another file whose name is pointed by
argv/2]. - You need to use the given buffer size of 100. If you change this, there will be
significant point reduction. - Exception handling is important. If your program doesn't handle
exceptions of system call failure - e.g., open 0 , read , write(), lseek ( - then you will get some
penalty. - You should not change the given template code except for the part designated to be
filled up by you. The following is for your own programming and debugging purposes. The only
file you need to submit to codePost.io is "copyReverse.c" In Ubuntu, you can compile this
program as was discussed in the class: $> gec -0 copyReverse copyReverse.c
In Ubuntu, you can compile this program as was discussed in the class: $> gec -0 copyReverse
copyReverse.c After the compilation you need to run your program with two file names as
commandline arguments such as: S>./copyReverse filel.txt file2.txt Then your program is
supposed to create another file "file2.txt" from the existing file "file1.txt" after replacing each
occurrence of a lower-case character with an upper-case character. For example, if your input
file, file1.txt, contains: GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant
knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY
bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, Then the outnut file file?
trt chould
"file1.txt" after replacing each occurrence of a lower-case character with an upper-case character.
For example, if your input file, file1.txt, contains: GAILY bedight, ABC A gallant knight,
GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight,
ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant
knight,

More Related Content

Similar to copyReverse.c code please do not change anything in the code bes.pdf

Unix primer
Unix primerUnix primer
Unix primer
dummy
 
Linuxppt
LinuxpptLinuxppt
Linuxppt
Reka
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Question 1.Discuss at least three ways in which Henry David Thor.docx
Question 1.Discuss at least three ways in which Henry David Thor.docxQuestion 1.Discuss at least three ways in which Henry David Thor.docx
Question 1.Discuss at least three ways in which Henry David Thor.docx
IRESH3
 

Similar to copyReverse.c code please do not change anything in the code bes.pdf (20)

Computer Security
Computer SecurityComputer Security
Computer Security
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Linux_Commands.pdf
Linux_Commands.pdfLinux_Commands.pdf
Linux_Commands.pdf
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Linuxppt
LinuxpptLinuxppt
Linuxppt
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Unix primer
Unix primerUnix primer
Unix primer
 
Linuxppt
LinuxpptLinuxppt
Linuxppt
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Bash shell
Bash shellBash shell
Bash shell
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
Question 1.Discuss at least three ways in which Henry David Thor.docx
Question 1.Discuss at least three ways in which Henry David Thor.docxQuestion 1.Discuss at least three ways in which Henry David Thor.docx
Question 1.Discuss at least three ways in which Henry David Thor.docx
 
Embed--Basic PERL XS
Embed--Basic PERL XSEmbed--Basic PERL XS
Embed--Basic PERL XS
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Hawk presentation
Hawk presentationHawk presentation
Hawk presentation
 
Scripting 101
Scripting 101Scripting 101
Scripting 101
 
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
 

More from murtuzadahadwala3

Create an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdfCreate an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdf
murtuzadahadwala3
 
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdfCreate a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
murtuzadahadwala3
 
Create an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdfCreate an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdf
murtuzadahadwala3
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
murtuzadahadwala3
 
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdfContabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
murtuzadahadwala3
 
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdfContaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
murtuzadahadwala3
 

More from murtuzadahadwala3 (20)

Create an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdfCreate an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdf
 
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdfCreate a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
 
Create an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdfCreate an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdf
 
Create a python program that creates a database in MongoDB using API.pdf
Create a python program that creates a database in MongoDB using API.pdfCreate a python program that creates a database in MongoDB using API.pdf
Create a python program that creates a database in MongoDB using API.pdf
 
Create a Java application that uses card layout with four cards with.pdf
Create a Java application that uses card layout with four cards with.pdfCreate a Java application that uses card layout with four cards with.pdf
Create a Java application that uses card layout with four cards with.pdf
 
Create a Class Diagram for a Rectangle class that has one constructo.pdf
Create a Class Diagram for a Rectangle class that has one constructo.pdfCreate a Class Diagram for a Rectangle class that has one constructo.pdf
Create a Class Diagram for a Rectangle class that has one constructo.pdf
 
Crane Company began the month of June with 1,630 units in beginning .pdf
Crane Company began the month of June with 1,630 units in beginning .pdfCrane Company began the month of June with 1,630 units in beginning .pdf
Crane Company began the month of June with 1,630 units in beginning .pdf
 
Count the number of occurrences of an item in a matrix. Create a Pyt.pdf
Count the number of occurrences of an item in a matrix. Create a Pyt.pdfCount the number of occurrences of an item in a matrix. Create a Pyt.pdf
Count the number of occurrences of an item in a matrix. Create a Pyt.pdf
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
 
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdfCOUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
 
convert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdfconvert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdf
 
Contrast the location of a food distributor and a supermarket. (The .pdf
Contrast the location of a food distributor and a supermarket. (The .pdfContrast the location of a food distributor and a supermarket. (The .pdf
Contrast the location of a food distributor and a supermarket. (The .pdf
 
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdfContabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
 
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdfContaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
 
Constructing Entity Relationship Diagram University workshop case st.pdf
Constructing Entity Relationship Diagram University workshop case st.pdfConstructing Entity Relationship Diagram University workshop case st.pdf
Constructing Entity Relationship Diagram University workshop case st.pdf
 
Consider the international strategy of a current entrepreneurial ve.pdf
Consider the international strategy of a current  entrepreneurial ve.pdfConsider the international strategy of a current  entrepreneurial ve.pdf
Consider the international strategy of a current entrepreneurial ve.pdf
 
Consider the network shown in the attached picture. Assume Distance .pdf
Consider the network shown in the attached picture. Assume Distance .pdfConsider the network shown in the attached picture. Assume Distance .pdf
Consider the network shown in the attached picture. Assume Distance .pdf
 
Consider the DE PBC Article. Which of the following sources of capit.pdf
Consider the DE PBC Article. Which of the following sources of capit.pdfConsider the DE PBC Article. Which of the following sources of capit.pdf
Consider the DE PBC Article. Which of the following sources of capit.pdf
 
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdfComputer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
 
Consider a population that grows according to the recursive rule Pn=.pdf
Consider a population that grows according to the recursive rule Pn=.pdfConsider a population that grows according to the recursive rule Pn=.pdf
Consider a population that grows according to the recursive rule Pn=.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 

copyReverse.c code please do not change anything in the code bes.pdf

  • 1. copyReverse.c code please do not change anything in the code besides the (four slashes ex.////) /* copyReverse -- copyReverse namel to name2 */ #include #include #include #include #include // !!! DO NOT CHNAGE BUFSIZE #define BUFSIZE 100 /* size of chunk to be read */ #define PERM 0644 /* file permission for new file */ // sawpping two char variables pointed by a and b // e.g., if you want to swap two variables, buf[i] and buf[j] // you should call "swap(&buffer[i], &buffer[j])" by passing references (i.e., addresses) void swap(char* a, char* b) { char t = *a; *a = *b; *b = t; } /* copyReverse namel to name2 */ int copyReverse( const char *namel, const char *name2) { int infile, outfile; ssize_t nread; char buffer[BUFSIZE]; if( ( infile = open(namel, O_RDONLY) ) == -1) return (- 1); if((outfile = open(name2,O_WRONLY|O_CREAT|O_TRUNC,PERM))==-1) { close (infile); return (-2); } // find out the size of file (name1) by using lseek call ////int fSize = lseek( ... ); // find out the number of blocks you need to read-reverse-write ////int numBlocks = ... // find out the size of partial block at the end ////int sizePartialBlock = ... // find the location of RWP to be moved if sizePartialBlock > 0 int begBlockIdx = fSize - sizePartialBlock; // if sizePartialBlock > 0, then you need to read it and reverse and write // you have to use lseek(), read(), swap(), and write() calls to do this //*** it is recommended to use SEEK_SET flag in lseek() call // //if(sizePartialBlock > 0) { //// //// //// //// //// } // Now, you need to reverse the remaining blocks from the end //(except for the PartialBlcok) // int cnt = 0; // to keep track of the number of blocks read/reversed while(cnt < numBlocks) { begBlockIdx = fSize - sizePartialBlock - ((cnt+1) * BUFSIZE); // move the read-writer pointer to point to the beginning of the // block to be read! You need to use lseek() //// //// // read one block bytes (100 bytes) into buffer //from the (numBlocks-cnt)-th block from the beginning of name1 file //// nread = read( ... ); //// // reverse bytes in buffer - you should use swap function declared above //for swapping two char variables. Note nread contains the actual number //of bytes read. //You may use additional extra array in this process - but there will //be some penalty if you do. Try to do it within buffer[] //without using additional memory! //// //// // write reversed buffer into name2 file - make sure to write only nread bytes //// //// cnt++; // increment the number of blocks read/reversed/written } // end of while loop! close(infile); close (outfile); return(0); // normal return } int main(int argc, char**argv) { if(argc!=3) { printf("Usage: copyReverse file1 file2 n"); exit(-1); } int retcode=0; retcode = copyReverse(argv[1], argv[2]); }the inputs and outputs below as examples input1.txt It was many and many a year ago, In a kingdom by the sea, That a maiden there lived whom you may know By the name of ANNABEL LEE;-- And this maiden she lived with no other thought Than to love and be loved by me. She was a
  • 2. child and I was a child, In this kingdom by the sea, But we loved with a love that was more than love-- I and my Annabel Lee-- With a love that the winged seraphs of heaven Coveted her and me. And this was the reason that, long ago, In this kingdom by the sea, A wind blew out of a cloud by night Chilling my Annabel Lee; So that her high-born kinsman came And bore her away from me, To shut her up in a sepulchre In this kingdom by the sea. The angels, not half so happy in Heaven, Went envying her and me:-- Yes! that was the reason (as all men know, In this kingdom by the sea) That the wind came out of a cloud, chilling And killing my Annabel Lee. But our love it was stronger by far than the love Of those who were older than we-- Of many far wiser than we- And neither the angels in Heaven above, Nor the demons down under the sea, Can ever dissever my soul from the soul Of the beautiful Annabel Lee:-- For the moon never beams without bringing me dreams Of the beautiful Annabel Lee; And the stars never rise but I see the bright eyes Of the beautiful Annabel Lee; And so, all the night- tide, I lie down by the side Of my darling, my darling, my life and my bride, In her sepulchre there by the sea-- In her tomb by the side of the sea. input2.txt From childhood's hour I have not been output1.txt .aes eht fo edis eht yb bmot reh nI --aes eht yb ereht erhclupes reh nI ,edirb ym dna efil ym ,gnilrad ym ,gnilrad ym fO edis eht yb nwod eil I ,edit-thgin eht lla ,os dnA ;eeL lebannA lufituaeb eht fO seye thgirb eht ees I tub esir reven srats eht dnA ;eeL lebannA lufituaeb eht fO smaerd em gnignirb tuohtiw smaeb reven noom eht roF --:eeL lebannA lufituaeb eht fO luos eht morf luos ym revessid reve naC ,aes eht rednu nwod snomed eht roN ,evoba nevaeH ni slegna eht rehtien dnA -ew naht resiw raf ynam fO --ew naht redlo erew ohw esoht fO evol eht naht raf yb regnorts saw ti evol ruo tuB .eeL lebannA ym gnillik dnA gnillihc ,duolc a fo tuo emac dniw eht tahT )aes eht yb modgnik siht nI ,wonk nem lla sa( nosaer eht saw taht !seY --:em dna reh gniyvne tneW ,nevaeH ni yppah os flah ton ,slegna ehT .aes eht yb modgnik siht nI erhclupes a ni pu reh tuhs oT ,em morf yawa reh erob dnA emac namsnik nrob-hgih reh taht oS ;eeL lebannA ym gnillihC thgin yb duolc a fo tuo welb dniw A ,aes eht yb modgnik siht nI ,oga gnol ,taht nosaer eht saw siht dnA .em dna reh detevoC nevaeh fo shpares degniw eht taht evol a htiW --eeL lebannA ym dna I --evol naht erom saw taht evol a htiw devol ew tuB ,aes eht yb modgnik siht nI ,dlihc a saw I dna dlihc a saw ehS .em yb devol eb dna evol ot nahT thguoht rehto on htiw devil ehs nediam siht dnA -- ;EEL LEBANNA fo eman eht yB wonk yam uoy mohw devil ereht nediam a tahT ,aes eht yb modgnik a nI ,oga raey a ynam dna ynam saw tI output2.txt neeb ton evah I ruoh s'doohdlihc morF (3) You are allowed to use only the system calls covered in the class (e.g., read 0 , write ), open 0 , Iseek (, close()). You can't use library calls such as fread 0 , fopen 0 , fwrite 0 , etc. If you don't follow this requirement, you will get significant penalty!! (4) Do not change other parts of your program except for the parts beginning with four slashes - there are multiple parts.
  • 3. Assignment Description: Given the template code, "copyReverse. c ", fill up the marked portion so that your program is performing the following tasks: - Read from input file whose name is pointed by argv[1]. - Write its reversed file contents into another file whose name is pointed by argv/2]. - You need to use the given buffer size of 100. If you change this, there will be significant point reduction. - Exception handling is important. If your program doesn't handle exceptions of system call failure - e.g., open 0 , read , write(), lseek ( - then you will get some penalty. - You should not change the given template code except for the part designated to be filled up by you. The following is for your own programming and debugging purposes. The only file you need to submit to codePost.io is "copyReverse.c" In Ubuntu, you can compile this program as was discussed in the class: $> gec -0 copyReverse copyReverse.c In Ubuntu, you can compile this program as was discussed in the class: $> gec -0 copyReverse copyReverse.c After the compilation you need to run your program with two file names as commandline arguments such as: S>./copyReverse filel.txt file2.txt Then your program is supposed to create another file "file2.txt" from the existing file "file1.txt" after replacing each occurrence of a lower-case character with an upper-case character. For example, if your input file, file1.txt, contains: GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, Then the outnut file file? trt chould "file1.txt" after replacing each occurrence of a lower-case character with an upper-case character. For example, if your input file, file1.txt, contains: GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight, GAILY bedight, ABC A gallant knight,