Homework 4 Strings Arrays and malloc CS2301 System



















![Code Fragment for Justify char *line[] = malloc(default. Arr. Size); int arr. Size = Code Fragment for Justify char *line[] = malloc(default. Arr. Size); int arr. Size =](https://slidetodoc.com/presentation_image_h2/b482819b01ae3296121b3d5a53563dd4/image-20.jpg)



- Slides: 23

Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non -majors (Slides include materials from The C Programming Language, 2 nd ed. , by Kernighan and Ritchie and from C: How to Program, 5 th ed. , by Deitel and Deitel) CS-2301 B-term 2008 Homework #4 1

Homework #4 • Due Friday, December 5, 11: 59 PM • Assignment: – – Read text from one or more files – Justify the text so that right and left margins line up – Print justified text • Somewhat bigger than previous homeworks CS-2301 B-term 2008 Homework #4 2

Objectives • Get and interpret arguments from the command line • Develop a program from multiple C files • Read input from file • Write output to file and/or stderr • Learn to use malloc(), realloc(), and free() • Work with strings and arrays of pointers CS-2301 B-term 2008 Homework #4 3

Background Reading in Kernighan & Ritchie • Chapter 7 – especially • § 7. 5, File input and output • § 7. 8, Miscellaneous functions • Chapter 4 • § 4. 11. 1, File inclusion • § 4. 11. 3, Conditional inclusion CS-2301 B-term 2008 Homework #4 4

Definition – Module • A C program that does not stand on its own • Not complete • Unable to execute when compiled • Requires other C programs to be compiled, linked, with it before it can run • (Usually) one of many C programs that together partition a large problem into small pieces • More tractable than one large program • (Often) can be tested independently of larger system CS-2301 B-term 2008 Homework #4 5

Program Structure for Homework #4 • At least three modules – hw 4. c • Contains the main() function and any utility functions • Reads and interprets command line – hw 4 Read. And. Print. c • Reads text from a file, invokes justify() function, prints justified lines – hw 4 Justify. c • Implements justify() function • To convert unjustified text to justified lines CS-2301 B-term 2008 Homework #4 6

Program Structure (continued) • Common include file – hw 4. h – Function headers for principle functions – Any common data structures • Included in each module – #include "hw 4. h" • Note straight quotes, instead of < > • Compiled with gcc –Wall –o hw 4. c hw 4 Read. And. Print. c hw 4 Justify. c CS-2301 B-term 2008 Homework #4 7

Program Structure (continued) • Common include file – hw 4. h – Function headers for principle functions – Any common data structures • Included in each module – #include "hw 4. h" • Note straight quotes, instead of • Compiled with d in e d clu ile? n i h p. m 4 o hw s to c t ’ n < > hy is of file W s list thi gcc –Wall –o hw 4. c hw 4 Read. And. Print. c hw 4 Justify. c CS-2301 B-term 2008 Homework #4 8

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file 2. txt. . . • Optional arguments • -w 100, -wn – specifies how wide the justified lines should be (in characters) • -t 5, -tn – specifies the tab spacing • Defaults to 80 characters wide and 5 character tabs • Mandatory arguments • File names • Each file contains text to justify • Sample files provided – see assignment CS-2301 B-term 2008 Homework #4 9

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file 2. txt. . . • The p Optional arguments the rogram nam – specifies nam “. / -wn • -w 100, how wide the justified lines e ” n of i e is eed characters) t should be (in ed? s file. simply Wh • -t 5, -tn – specifies they itab s spacing • Defaults to 80 characters wide and 5 character tabs • Mandatory arguments • File names • Each file contains text to justify • Sample files provided – see assignment CS-2301 B-term 2008 Homework #4 10

Definition – Justify text • Partition text into lines • No more characters than line width • Insert additional spaces to align right edges • Except if text ends with '