Programming Assignment 6 Strings and Dynamic Memory Allocation

  • Slides: 18
Download presentation
Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming for Non-Majors (Slides

Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 1

Assignment • Read in a series of text files • For each file, print

Assignment • Read in a series of text files • For each file, print text to fit on lines • Line width and tab spacing specified on command line • Justify each line • I. e. , make right margins line up Due Date: – Sunday, December 13, 2009, 11: 59 PM CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 2

Goals and Objectives • Pull together a non-trivial program from resources and algorithms at

Goals and Objectives • Pull together a non-trivial program from resources and algorithms at your disposal • Get input from files, write output to stdout or stderr • Use malloc() and free() to manage dynamically allocated arrays and strings • Complete a project in Visual Studio CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 3

By the End of this Assignment… • … you should feel more confident that

By the End of this Assignment… • … you should feel more confident that – You can write a non-trivial C program for any course assignment at WPI – You are capable of learning the things you don’t know on your own – You are beginning to think “computationally” with respect to collecting and organizing data CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 4

Reading and Review • Chapter 7 • Especially § 7. 5 and § 7.

Reading and Review • Chapter 7 • Especially § 7. 5 and § 7. 8 • Lab Assignments #6 (this week) and #7 (next week) CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 5

Your Program • Multiple files as in previous programming assignments • Of your own

Your Program • Multiple files as in previous programming assignments • Of your own organization • Visual Studio only • Algorithms from K & R or any other resources • Cite your resources! !s! d a n i e d i ipfre r k u i o en y. W Ev CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 6

Command Line of Your Program. /PA 6 –w 100 –t 5 file 1. txt

Command Line of Your Program. /PA 6 –w 100 –t 5 file 1. txt file 2. txt. . . Opt iona l. E ithe r or der • Default line width is 80 characters • Default tab spacing if 5 characters CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 7

Main Program • Read and process command line arguments • i. e. , argc

Main Program • Read and process command line arguments • i. e. , argc and argv • For each argument • Set line width or tab spacing or • Open an input file • Call Read. And. Print() for file • • Input – the newly opened file Output – stdout Error and other output – stderr Print file name on stderr CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 8

Read. And. Print() void Read. And. Print(FILE *input, FILE *output, const int width, const

Read. And. Print() void Read. And. Print(FILE *input, FILE *output, const int width, const int tab); Loop: – – Read one line using Read. Line() • Returns pointer to character array – Justify() the line • Replaces character array with a new one – Print the line on *output – free() the character array CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 9

Read. Line() char *Read. Line(FILE *input, const int width, const int tab); Difficult function:

Read. Line() char *Read. Line(FILE *input, const int width, const int tab); Difficult function: – – malloc() a new character array – Read one character at a time – Break at word boundaries – Expand tabs – Append '' at end – Return character array CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 10

Read. Line() [continued] • If newline detected, append 'n' before '�' • Strip white

Read. Line() [continued] • If newline detected, append 'n' before '' • Strip white space after last word • So next word starts in first column • Exception: – if previous line ends in 'n', don’t strip white space (to allow for paragraph indenting) • If 'n' detected in white space after last word • Append 'n' before '' CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 11

Justify() • Optional — for extra credit char *Justify(const char *text, const int width);

Justify() • Optional — for extra credit char *Justify(const char *text, const int width); • Takes input line ≤ width characters • Creates new line exactly width characters • So that right margins line up • malloc() new character array • Copy and insert space at random • free() original character array • If original line ends in 'n' • Return without creating new line CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 12

Printing (in main loop) • If line does not end in 'n' • Be

Printing (in main loop) • If line does not end in 'n' • Be sure to print 'n' • If line does end in 'n' • Don’t append another one CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 13

Read. Line() – End of File When EOF is encountered in input: – •

Read. Line() – End of File When EOF is encountered in input: – • If you still have unreturned characters in character array – append 'n' and return array • If input array is empty – Return null pointer CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 14

Write-up • Description of your program, the. c files, . h files, etc. •

Write-up • Description of your program, the. c files, . h files, etc. • Loop invariants of critical loops • Description of your data structure and the principal functions • Citation of resources and algorithms CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 15

Visual Studio • You must submit this project in Visual Studio • Take advantage

Visual Studio • You must submit this project in Visual Studio • Take advantage of debugger • Project is too hard to do without a debugger • Be sure to clean your Visual Studio project before submitting • Zip files together • But keep README file separate CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 16

Submitting Your Project • Use /cs/bin/turnin submit cs 2301 PA 6. . . •

Submitting Your Project • Use /cs/bin/turnin submit cs 2301 PA 6. . . • Submit: – • Write-up • Clean, zipped version of Visual Studio project directory • Sample output CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 17

Questions? CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 18

Questions? CS-2301, B-Term 2009 Strings and Dynamic Memory Allocation 18