Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings and Dynamic Memory Allocation CS-2301, B-Term 20091 Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.

Similar presentations


Presentation on theme: "Strings and Dynamic Memory Allocation CS-2301, B-Term 20091 Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming."— Presentation transcript:

1 Strings and Dynamic Memory Allocation CS-2301, B-Term 20091 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)

2 Strings and Dynamic Memory Allocation CS-2301, B-Term 20092 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

3 Strings and Dynamic Memory Allocation CS-2301, B-Term 20093 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

4 Strings and Dynamic Memory Allocation CS-2301, B-Term 20094 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

5 Strings and Dynamic Memory Allocation CS-2301, B-Term 20095 Reading and Review Chapter 7 Especially §7.5 and §7.8 Lab Assignments #6 (this week) and #7 (next week)

6 Strings and Dynamic Memory Allocation CS-2301, B-Term 20096 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!

7 Strings and Dynamic Memory Allocation CS-2301, B-Term 20097 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

8 Strings and Dynamic Memory Allocation CS-2301, B-Term 20098 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

9 Strings and Dynamic Memory Allocation CS-2301, B-Term 20099 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

10 Strings and Dynamic Memory Allocation CS-2301, B-Term 200910 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

11 Strings and Dynamic Memory Allocation CS-2301, B-Term 200911 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'

12 Strings and Dynamic Memory Allocation CS-2301, B-Term 200912 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

13 Strings and Dynamic Memory Allocation CS-2301, B-Term 200913 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

14 Strings and Dynamic Memory Allocation CS-2301, B-Term 200914 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

15 Strings and Dynamic Memory Allocation CS-2301, B-Term 200915 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

16 Strings and Dynamic Memory Allocation CS-2301, B-Term 200916 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

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

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


Download ppt "Strings and Dynamic Memory Allocation CS-2301, B-Term 20091 Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming."

Similar presentations


Ads by Google