Strings and Dynamic Memory Allocation CS-2301, B-Term 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)
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term Reading and Review Chapter 7 Especially §7.5 and §7.8 Lab Assignments #6 (this week) and #7 (next week)
Strings and Dynamic Memory Allocation CS-2301, B-Term 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! Even Wikipedia!Even your friends!
Strings and Dynamic Memory Allocation CS-2301, B-Term Command Line of Your Program./PA6 –w100 –t5 file1.txt file2.txt... Default line width is 80 characters Default tab spacing if 5 characters Optional. Either order
Strings and Dynamic Memory Allocation CS-2301, B-Term 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 ReadAndPrint() for file Input – the newly opened file Output – stdout Error and other output – stderr Print file name on stderr
Strings and Dynamic Memory Allocation CS-2301, B-Term ReadAndPrint() void ReadAndPrint(FILE *input, FILE *output, const int width, const int tab); Loop:– –Read one line using ReadLine() Returns pointer to character array –Justify() the line Replaces character array with a new one –Print the line on *output –free() the character array
Strings and Dynamic Memory Allocation CS-2301, B-Term ReadLine() char *ReadLine(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 '\0' at end –Return character array
Strings and Dynamic Memory Allocation CS-2301, B-Term ReadLine() [continued] If newline detected, append '\n' before '\0' 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 '\0'
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term ReadLine() – 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
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term 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
Strings and Dynamic Memory Allocation CS-2301, B-Term Submitting Your Project Use /cs/bin/turnin submit cs2301 PA6... Submit:– Write-up Clean, zipped version of Visual Studio project directory Sample output
Strings and Dynamic Memory Allocation CS-2301, B-Term Questions?