\documentclass[12pt]{article} \usepackage{times} \usepackage{fancyheadings} \pagestyle{fancy} \usepackage{pifont} % for fancy bullets \newcommand{\headerfont}{\fontfamily{phv}\fontseries{b}\fontshape{n}% \fontsize{10}{12pt}\selectfont} \newcommand{\ttb}{\fontfamily{pcr}\fontseries{b}\fontshape{n}% \fontsize{12}{14pt}\selectfont} \newcommand{\ttsb}{\fontfamily{pcr}\fontseries{b}\fontshape{n}% \fontsize{8}{10pt}\selectfont} \newcommand{\tts}{\fontfamily{pcr}\fontseries{m}\fontshape{n}% \fontsize{8}{10pt}\selectfont} \newcommand{\largebold}{\fontfamily{ptm}\fontseries{b}\fontshape{n}% \fontsize{14}{16pt}\selectfont} \newcommand{\Largebold}{\fontfamily{ptm}\fontseries{b}\fontshape{n}% \fontsize{19}{21pt}\selectfont} \newcommand{\Largeboldital}{\fontfamily{ptm}\fontseries{b}\fontshape{it}% \fontsize{19}{21pt}\selectfont} \newcommand{\largeboldital}{\fontfamily{ptm}\fontseries{b}\fontshape{it}% \fontsize{14}{16pt}\selectfont} \newcommand{\mywidth}{6.0in} \newcommand{\st}{\rule[-.8mm]{0mm}{2mm}} \newcommand{\longst}{\rule[-3mm]{0mm}{2mm}} \newcommand{\outerbaselinesep}{12.5pt} \topmargin 0.0in \oddsidemargin 0.25in \evensidemargin 0.25in \textwidth \mywidth \textheight 8.5in \parindent 0mm %10mm \parskip 2.5mm \fboxrule=0.3mm %%%%%%%%% Headings %%%%%%%%%%%%%%%%%%%%%%% \setlength{\headwidth}{\textwidth} % This shouldn't be needed. ?? %\setlength{\headrulewidth}{0pt} % to eliminate rule under header \newcommand{\currhead}{CS 1723, Assignment 4: Left Justify Text, \today} \lhead{\footnotesize \currhead} \rhead{\footnotesize Page \thepage~~of~~\pageref{'thatsall'}\hspace{0.4mm}} \lfoot{} \chead{} \cfoot{} \rfoot{} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} \begin{center} {\largebold CS 1723, Data Structures \\ Spring Semester, 1998 \\ Programming Assignment 4, Due September 25, 1998 } \\ {\largeboldital Left Justify Text} \end{center} \baselineskip=\outerbaselinesep {\bf The Assignment.} For this assignment, you are to write a program that will produce text that is lined up at the left (left justified). Specifically, your program should read an integer ({\tt linelen}) for the number of columns to use, and should read the name of a file for the text. The program should then read the file, and should arrange the words of the file into lines of length no longer than {\tt linelen}. Here is a sample output of this program. Your program should produce the ``ruler'' across the top as shown and should include two runs, with widths 75 and 20, as shown. {\tts runner\% {\ttsb lj}}\\ {\ttsb 75 lewis.text} \vspace{-0.2in} {\scriptsize \begin{verbatim} 1 2 3 4 5 6 7 123456789012345678901234567890123456789012345678901234567890123456789012345 --------------------------------------------------------------------------- What you have made me see is as plain as the sky, but I never saw it before. Yet it has happened every day. One goes into the forest to pick food and already the thought of one fruit rather than another has grown up in one's mind. Then, it may be, one finds a different fruit and not the fruit one thought of. One joy was expected and another is given. But this I had never noticed before--that the very moment of the finding there is in the mind a kind of thrusting back, or setting aside. The picture of the fruit you have _not_ found is still, for a moment, before you. And if you wished--if it were possible to wish--you could keep it there. You could send your soul after the good you had expected, instead of turning it to the good you had got. You could refuse the real good; you could make the real fruit taste insipid by thinking of the other. C.S. Lewis, Perelandra, 1944 \end{verbatim} } \vspace{-0.2in} {\tts runner\% {\ttsb lj}}\\ {\ttsb 20 lewis.text} \vspace{-0.2in} {\scriptsize \begin{verbatim} 1 2 12345678901234567890 -------------------- What you have made ... (lines missing) than another has grown up in one's mind. Then, it may be, one finds a different fruit and \end{verbatim} } {\bf Details.} \begin{enumerate} \item Read the number of columns ({\tt linelen}) and the name of a file ({\tt infilename}) using {\tt scanf} and formats ``\verb+%i+'' and ``\verb+%s+''. Reading the file name, opening the file with that name, and reading from that file can look as follows: \begin{verbatim} FILE *infile; scanf("%i %s", &linelen, infilename); if ((infile = fopen(infilename, "r")) == NULL) { printf("Couldn't open file: %s\n", infilename); exit(1); } fscanf(infile, ... ); \end{verbatim} Getting the name of file wrong is common in programs, so an {\tt fopen} should always check that the value returned is not {\tt NULL} (which indicates that the file did not open). \item This program regards the file of text as a sequence of words, where ``word'' means any sequence of non-whitespace characters. Thus successive words are separated by whitespace: characters that don't show up when printed, such as a blank (\verb+' '+), a newline (\verb+'\n'+), tab (\verb+'\t'+), form feed (\verb+'\f'+), and vertical tab(\verb+'\v'+). The function {\tt isspace()} returns true for whitespace characters, but you don't necessarily need it, because the function {\tt scanf} ignores whitespace characters on input, so that {\tt scanf} with a ``\verb+%s+'' format will fetch successive words as strings with a \verb+'\0'+ at the end. \item You can fetch word at a time, and make up a line of output with as many words as possible in it, with just one blank between each word, and not taking up more than {\tt linelen} number of characters. Then the program outputs these lines. The result will be left justified. You should also output the initial scale exactly as shown. \item There are two strategies for the basic program, that is for making up lines of length less than or equal to {\tt linelen} (where each successive pair of words has exactly one blank between them). One strategy uses C string functions and works with strings as a whole. The other strategy regards the strings as arrays of char, and works with them character at a time. Either strategy, or even a mixed strategy, is valid. \item Both {\tt fscanf(infile, ...)} and {\tt fgetc(infile)} return {\tt EOF} at end-of-file. If you have trouble with {\tt EOF}, you could use a special word, such as ``{\tt \#END\#}'' at the end as sentinel. \end{enumerate} {\bf What to turn in.} Turn in a source listing and the results of at least two runs of the program. One with {\tt linelen} 75 and the other with {\tt linelen} 20. You may use any text that you choose. Try to find something interesting, but please nothing in ``poor taste.'' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{'thatsall'} \end{document}